Greed me to death-HRBUST-1167-How much is each denomination of currency

Problem describe

The organization finally paid the salary, and the salary that had been waiting for a long time finally came. . .
In order to allow everyone to receive wages as quickly as possible, the organization decided to pay all the wages at once, and there will be no change for employees. That is to say, if an employee’s salary is 1,160 yuan, 11 100 will be given. Yuan, one card is 50 yuan, and one card is 10 yuan, instead of giving employees 1,200 yuan, and then asking employees to find 40 yuan in change.
The wages of employees are all integers, the unit is yuan, and the face value of RMB 100, 50, 20, 10, 5, and 1 on the market.
The minimum number of banknotes required in the end is required.

Input

There are multiple sets of test data, each set of test data occupies one line.
For each set of test data, the first number n indicates how many employees the organization has, and the next n numbers indicate how much salary each employee has to pay.
Process to the end of the file.
1 <= n <= 100000, the salary of each employee does not exceed 1000000

Output

Output 6 numbers per line, indicating how many sheets are required for each of 100 yuan, 50 yuan, 20 yuan, 10 yuan, 5 yuan, and 1 yuan.
The answer may be 0.

Sample Input

1 701
3 474 808 212

Sample Output

7 0 0 0 0 1
14 1 1 1 1 9


The meaning of the question: how to choose to make the minimum number of banknotes paid.

Greedy template questions, just go to death and greedy. At first, I thought of using for+subtraction to choose the best solution. After thinking about it, I found that using division+remainder can solve the problem more efficiently.

It should be noted that every time you input data, you must be greedy and add the results, instead of greedy outputting the results after adding the data .


#include<iostream>
using namespace std;
int main() {
    
    
	ios::sync_with_stdio(false); 
	int n; while(cin>>n) {
    
    
		int num=0, a_100=0, a_50=0, a_20=0, a_10=0, a_5=0, a_1=0; 
		for(int i = 0; i < n; i++) {
    
    
			cin>>num;		
			a_100+=num/100;  num%=100;
			a_50+=num/50;	num%=50;
			a_20+=num/20;	num%=20;
			a_10+=num/10;	num%=10;
			a_5+=num/5;		num%=5;
			a_1+=num/1; 	num%=1;
		}
		cout<<a_100<<' '<<a_50<<' '<<a_20<<' '<<a_10<<' '<<a_5<<' '<<a_1<<endl;
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/108284196