10018. Jinjin's savings plan

Posted by mmj on Sun, 19 Sep 2021 15:08:47 +0200

Jinjin's pocket money has always been managed by itself. At the beginning of each month, my mother gives Jinjin 300 yuan. Jinjin will budget the expenses of this month, and always achieve the same actual expenses as the budget.
In order to let Jinjin learn how to save, her mother proposed that Jinjin could deposit 100% of her money at any time, and she would return it to Jinjin with 20% at the end of the year. Therefore, Jinjin has formulated a savings plan: at the beginning of each month, after receiving the pocket money given by her mother, if she expects that there will be more than 100 yuan or exactly 100 yuan in her hand by the end of this month, she will deposit the whole 100 yuan with her mother and leave the rest in her own hands.
For example, in early November, there was 83 yuan in Jinjin's hand, and her mother gave 300 yuan to Jinjin. Jinjin expects to spend 180 yuan in November, so she will save 200 yuan in her mother and leave 183 yuan by herself. By the end of November, Jinjin will have 3 yuan left.
Jinjin found that the main risk of the savings plan was that the money stored in her mother could not be withdrawn before the end of the year. It is possible that at the beginning of a month, the money in Jinjin's hand plus the money given by his mother this month is not enough for the original budget of this month. If this happens, Tianjin will have to live frugally and reduce its budget this month.
Now please judge whether this will happen according to the monthly budget of Tianjin from January to December 2004. If not, by the end of 2004, how much money will there be in Jinjin's hands after my mother returns 20% of the money normally saved in Jinjin to Jinjin.

Input format:

The input includes 12 rows of data, and each row contains a non negative integer less than 350, representing the budget of Tianjin from January to December respectively.

Output format:

The output consists of a line that contains only one integer. If there is insufficient money in a certain month during the implementation of the savings plan, output - x, and X represents the first month of this situation; Otherwise, how much money will there be in Jinjin's hands by the end of 2004.

Tips:

NOIp 2004

Limitations:

1s per test point

Example 1:

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

Example 2:

Input:
290
230
280
200
300
170
330
50
90
80
200
60
Output:
1580

  Problem solution 1:

#include <iostream>
using namespace std;

int main() {
	int budget[12] = { 0 }; //Jinjin's 12-month budget
	int sum = 0;          //Amount of money in Tianjin as of the current month

	for (int &temp :
		budget) //Operate on each element in the budget as follows (read data)
		cin >> temp; //If you want to write data, you need to associate & temp with the elements in the array

	int sumOfBudget = 0;//Total deposit budget

	//1. In case of insufficient money:
	for (int i = 0; i < 12; i++) {
		sum = sum + 300 - budget[i]; //Amount of money in Tianjin as of the current month
		sumOfBudget += budget[i];    //Cumulative sum of budget
		sum = sum % 100;             //The amount of money in Tianjin by the end of the current month (the amount that should be saved to my mother with Yu Xie, because 100 is saved to my mother)

		if (sum < 0) //If the monthly money is less than 0
		{
			cout << "-" << i + 1<< endl; //Since budget[0] is saved in the first month, the output month will be i+1
			return 0; //If the money is not enough, which month is negative, end the program
		}
	}
	//2. When the money is enough:
	cout << (300 * 12 - sumOfBudget) % 100 +(300 * 12 - sumOfBudget) / 100 * 100 * 12 / 10;
	return 0;
}

Problem solution 2:

#include <iostream>
using namespace std;

int main()
{
	int budget[12] = { 0 };					//Jinjin's 12-month budget
	int sum = 0;							//Amount of money in Tianjin as of the current month
	int deposit = 0;						//Deposits in Tianjin as of the current month

	for (int &temp : budget)				//Operate on each element in the budget as follows (read data)
		cin >> temp;						//If you want to write data, you need to associate & temp with the elements in the array

	for (int i = 0; i < 12; i++)
	{
		sum = sum + 300 - budget[i];		//Amount of money in Tianjin as of the current month

		if (sum >= 100)						//If the monthly money is greater than or equal to 100, you need to deposit
		{
			deposit = deposit + sum / 100 * 100;		//Deposits in Tianjin as of the current month
			sum = sum - sum / 100 * 100;				//The amount of money in Tianjin as of the current month is not counted as the deposit
		}
		else if (sum < 0)					//If the monthly money is less than 0
		{
			cout << "-" << i + 1 << endl;   //Since budget[0] is saved in the first month, the output month will be i+1
			return 0;						//If the money is not enough, which month is negative, end the program
		}
	}

	cout << sum + deposit * 12 / 10;       //The last amount of money not deposited in Tianjin + the number of deposits * 12 / 10
	return 0;
}

Topics: C++