An exam through the first part of the C ++ language -> Chapter IV loop structure of programming 1074 Jinjin savings plan

1074: Jinjin savings plan


Time limit: 1000 ms Memory Limit: 65536 KB
Submissions: 30997 Number through: 13382 

Description [title]

Jinjin pocket money has always been its own management. Beginning of each month to the mother Jinjin 300 yuan, Jin-Jin will be spending budget this month, and always do the same actual spending and budget.

In order to learn how to save Jinjin, my mother suggested that the presence of Jinjin can always put her money where hundreds whole, to the end of the year she will add a 20% return Jinjin. Therefore Jinjin develop a savings plan: start of each month, after the mother to get pocket money, if she expected the end of this month there will be in the hands of just more than $ 100 or $ 100, she will put 100th mom's money in there, the rest of the money in their hands.

In early November, for example, there are 83 yuan Jinjin hands, my mother gave Jinjin 300 yuan. Jinjin expected in November spending was $ 180, then she will save 200 yuan mother where he left 183 yuan. To the end of November, it will be left in the hands of Jinjin 3 dollars.

Now you based on January 2004 to December Jinjin budget each month, the judge will not happen. If after not calculated to the end of 2004, my mother would Jinjin usual deposit money back plus 20% of the Jin-Jin, Jin-Jin hands will have much money.

[Enter]

12 includes the line data, each line contains a non-negative integer less than 350, respectively, from January to December Jinjin budget.

[Output]

Only contains an integer. The situation appeared a month short of money during the savings plan, if implemented, output -X, X represents the emergence of the first month of this situation; otherwise the output to the end of 2004, Jinjin hands will have much money.

 

Source

 

In this problem by reference "p", is determined as a variable, if the situation does not occur with enough money, then p 1 is a constant, where once enough money occurs, then the value of p to 0, to ensure that the recorded month first appeared in January, but in fact the case with If there is not enough money, you can jump directly for loop optimization program.

#include<iostream>using namespace std;int main(){ int n,c=0,p=1,m,w=0,t,s;  for(int i=1;i<=12;i++)  { cin>>n;    c=c+300-n;    if(c<0&&p==1)    { m=i;      p=0;}t=c/100;c=c-t*100;w+=t;  }  if(p==0)   cout<<(-1)*m<<endl;  else  { s=w*120+c;    cout<<s<<endl;   }   return 0;  } 

Guess you like

Origin www.cnblogs.com/jd1412/p/12239353.html