#include <stdio.h> int main() { int tarShut=200; int tarshut[9]={0},i; int a=700; int b=10000; int c= 200; int d=25; int tmp1=-600; int tmp2; for(i=0;i<5;i++) { tmp2=tmp1; if(tmp2>50) { tmp2=tmp1-a; if(tmp2>0) { tmp1=tmp2; tarshut[i]=b; } else tarshut[i]=0; a=a-a*(i+1)/4; b=b/2; } else if(tmp2<-50) { tmp2=tmp1+a; if(tmp2<0) { tmp1=tmp2; tarshut[i]=b; } else tarshut[i]=0; a=a-a*(i+1)/4; b=b/2; } } for(i=5;i<9;i++) { tmp2=tmp1; if(tmp2<=50&&tmp2>0) { tmp2=tmp1-d; if(tmp2>0) { tmp1=tmp2; tarshut[i]=c; } else tarshut[i]=tarShut; d=d-d*(i+1)/5; c=c/2; } if(tmp2<0&&tmp2>=-50) { tmp2=tmp1+d; if(tmp2<0) { tmp1=tmp2; tarshut[i]=c; } else tarshut[i]=tarShut; d=d-d*(i+1)/5; c=c/2; } } for(i=0;i<9;i++) printf("tarshut[%d]=%d\n",i,tarshut[i]); for(i=0;i<8;i++) { if ((tarshut[i] - tarshut[i + 1]) != 0) { if ((tarshut[i] > tarshut[i + 1]) ) printf("tarshut[%d]=%d\n",i,tarshut[i]); else printf("tarshut[%d]=%d\n",i+1,tarshut[i+1]); return 0; } } return 0; }改进上述代码
时间: 2024-04-12 17:31:40 浏览: 13
对于上述代码的改进,可以考虑以下几个方面:
1. 减少重复代码:在第一个循环中,可以将 tmp2>50 和 tmp2<-50 的情况合并为一个条件,减少重复代码。
2. 减少不必要的判断:在第一个循环中,可以直接将 tarshut[i] 的赋值放在循环外部进行,避免每次循环都进行判断。
3. 优化变量命名:变量命名应该具有描述性,能够清晰地表达其含义。
下面是对代码的改进后的版本:
```c
#include <stdio.h>
int main()
{
int tarShut = 200;
int tarshut[9] = {0};
int i;
int a = 700;
int b = 10000;
int c = 200;
int d = 25;
int tmp1 = -600;
int tmp2;
for (i = 0; i < 5; i++)
{
tmp2 = tmp1;
int decreaseA = a * (i + 1) / 4;
if (tmp2 > 50 || tmp2 < -50)
{
tmp2 -= (tmp2 > 50) ? decreaseA : -decreaseA;
if ((tmp2 > 0 && tmp2 <= 50) || (tmp2 < 0 && tmp2 >= -50))
{
tmp1 = tmp2;
tarshut[i] = b;
}
else
{
tarshut[i] = 0;
}
a -= decreaseA;
b /= 2;
}
}
for (i = 5; i < 9; i++)
{
tmp2 = tmp1;
int decreaseD = d * (i + 1) / 5;
if (tmp2 <= 50 && tmp2 > 0)
{
tmp2 -= decreaseD;
if (tmp2 > 0)
{
tmp1 = tmp2;
tarshut[i] = c;
}
else
{
tarshut[i] = tarShut;
}
d -= decreaseD;
c /= 2;
}
else if (tmp2 < 0 && tmp2 >= -50)
{
tmp2 -= -decreaseD;
if (tmp2 < 0)
{
tmp1 = tmp2;
tarshut[i] = c;
}
else
{
tarshut[i] = tarShut;
}
d -= decreaseD;
c /= 2;
}
}
for (i = 0; i < 9; i++)
{
printf("tarshut[%d]=%d\n", i, tarshut[i]);
}
for (i = 0; i < 8; i++)
{
if ((tarshut[i] - tarshut[i + 1]) != 0)
{
if ((tarshut[i] > tarshut[i + 1]))
printf("tarshut[%d]=%d\n", i, tarshut[i]);
else
printf("tarshut[%d]=%d\n", i + 1, tarshut[i + 1]);
return 0;
}
}
return 0;
}
```
通过以上改进,代码更加简洁,减少了重复代码和不必要的判断。同时,代码仍然保持了原有的逻辑。
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)