@[dayaojing](/user/1165047) 你干嘛要写一堆 `if` 语句判断上缴多余的钱,用取模不就好了(虽然可能不是错在这了,我再看看
by 半只蒟蒻 @ 2023-11-02 21:48:24
@[dayaojing](/user/1165047) 好像改着改着就对了,你看看哪错了
```cpp
//帮别人调试代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int left =0, save = 0, cost, month = 1,sum;
for (month = 1; month <13; month++){
left+=300;
scanf("%d", &cost);
if (cost > left)
{
printf("%d", -month);
return 0;
}
else
left -= cost;
if(left>=300)
{
save+=300;
left-=300;
}
else if (left >= 200)
{
save += 200;
left = left - 200;
}
else if (left >= 100)
{
save += 100;
left = left - 100;
}
}
sum=save * 1.2 + left;
printf("%d",sum);
return 0;
}
``````
by pjfffb @ 2023-11-02 21:54:39
思路没问题,细节上出问题了:`printf("%d", save * (1 + 0.2) + left);`
**%d是int类型,但save乘了一个1.2,虽然还是整数,但程序会判定它为double类型,加上left也一样**
这样类型冲突,就会发生的问题,不信你跑一遍样例2,它会输出0
改正方法:直接把%d换成%.0f就行:`printf("%.0f", save * (1 + 0.2) + left);`
AC代码:
```c
#include<stdio.h>
int main()
{
int left =0, save = 0, cost, month = 1;
for (month = 1; month <13; month++)
{ left+=300;
scanf("%d", &cost);
if (cost > left)
{
printf("%d", -month);
break;
}
else
left -= cost;
if(left>=300)
{
save+=300;
left-=300;
}
else if (left >= 200)
{
save += 200;
left = left - 200;
}
else if (left >= 100)
{
save += 100;
left = left - 100;
}
else
save += 0;
if(month==12)
printf("%.0f", save * (1 + 0.2) + left);
}
return 0;
}
```
by zhang_feng_rui @ 2023-11-02 21:55:42
@[半只蒟蒻](/user/112049) @[zhang_feng_rui](/user/1061189) 解决了谢谢
by dayaojing @ 2023-11-03 19:03:39
@[pjfffb](/user/1057695) 解决了谢谢
by dayaojing @ 2023-11-03 19:04:06