仅供参考。
题目链接P1089 [NOIP2004 提高组] 津津的储蓄计划 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<stdlib.h>
#include<stdio.h>
int main()
{
int budget=0,total=0,R=0,x=0;
for(int i=1;i<=12;i++)
{
scanf("%d",&budget);
if(budget<0||budget>350) //限制输入值范围
{
printf("你输入的数据有误");
exit(1);
}
int h=(300-budget+R)/100; //计算减去预算后的数的百位数
if(budget>(300+R)) //判断津津是否能付得起这个月的预算
x=i;
switch(h)
{
case 0 :R=(300-budget+R); //百位数为0,则减去预算后的钱下个月使用
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:total+=((300-budget+R)/100)*100;R=(300-budget+R)-((300-budget+R)/100)*100;
break; //计算向妈妈那里存放钱的数值并求总和 多余的零头下个月继续使用
}
}
int m=total*(1+0.2)+R; //求津津年末能拿多少钱
printf("年末津津可以得到%d\n -x=-%d",m,x);
return 0;
}
如果有可以优化的地方或其他方法欢迎评论区交流或者私信我交流。