Luogu brush questions C++ language | P1089 Jinjin's savings plan

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Jinjin’s pocket money has always been managed by himself. At the beginning of each month, my mother gives Jinjin 300 yuan, and Jinjin will budget the expenses of this month, and the actual expenses will always be the same as the budget.

In order for Jinjin to learn how to save, her mother proposed that Jinjin can deposit the whole hundred dollars with her at any time, and she will add 20% to Jinjin at the end of the year. So Jinjin made a savings plan: at the beginning of each month, after receiving the pocket money from her mother, if she expects to have more than 100 yuan or exactly 100 yuan by the end of the month, she will put the whole hundred Most of the money is deposited with my mother, and the rest of the money is kept in my own hands.

For example, at the beginning of November, Jinjin still had 83 yuan in his hands, and his mother gave Jinjin 300 yuan. Jinjin expects to spend 180 yuan in November, so she will save 200 yuan with her mother and keep 183 yuan for herself. By the end of November, Jinjin will have 3 yuan left in his hand.

Jinjin found that the main risk of this savings plan is that the money deposited with her mother cannot be withdrawn before the end of the year. It is possible that at the beginning of a certain month, the money in Jinjin’s hands plus the money given by her mother this month is not enough for the original budget for this month. If this happens, Jinjin will have to save money and squeeze the budget this month.

Now please judge whether this will happen based on the monthly budget of Jinjin from January to December 2004. If not, by the end of 2004, calculate how much money Jinjin will have in his hands after his mother repays Jinjin's usual savings plus 20%.

【enter】

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

【Output】

an integer. If there is insufficient money in a certain month during the implementation of the savings plan, output − X , where X  represents the first month when this situation occurs; otherwise, how much money will be output to Jinjin at the end of 2004.

Note that Luogu does not need to perform file input and output, but standard input and output.

【Input sample】

290 230 280 200 300 170 340 50 90 80 200 60

【Example of output】

-7

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int ans=0, mother=0, yusuan;
    for (int i=1; i<=12; i++) {
        cin >> yusuan;
        ans = ans + 300 - yusuan;
        if (ans<0) {
            printf("%d", -i);
            return 0;
        }
        mother += ans / 100;  //几张100元钱
        ans = ans % 100;
    }
    printf("%d", ans+mother*120);
    return 0;
}

【operation result】

290
230
280
200
300
170
340
50 
90 
80 
200
60 
-7

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132642422