解法1:
#include<iostream>
using namespace std; 使用bool变量
int main()
{
int a,i,t=0,n=0;bool f=1; //a为预算,t存津津留在手中的钱,n存津津存在妈妈手上的钱;
for(i=1;i<=12;i++)
{ cin>>a;
t=t+300-a;
if(t<0)
{ cout<<"-"<<i;
f=0;
break;}
if(t>=100)
{ n=n+t/100; //把整百的钱存在妈妈那里,剩余的钱留在自己手中;
t%=100; }
}
if(f==1)
cout<<n*120+t; //妈妈将津津平常存的钱加上20%还给津津;n*100*1.2
return 0;
}
解法2:
#include<iostream> 灵活利用flag
using namespace std;
int main()
{
int a,i,t=0,n=0,flag=1,month;
for(int i=1;i<=12;i++)
{
cin>>a;
t=t+300-a;
if(t<0)
{
flag=0;
month=i;
break;
}
n+=t/100; //剩余的钱整除100即为在妈妈手里的100元的张数
t%=100; //用100去模剩余的钱即为月底幸存的钱
}
if(flag==1)
cout<<n*120+t;
else
cout<<-month;
return 0;
}