JakeLin- [ACM Training] -Pay Salary-Problem Solution / Greedy / Water Problem

Title description

As a programmer, the most anticipated day is the 9th of every month, because this day is the day to pay, and it depends on it to support the family, huh, 
but for the staff of the company's finance department, this day is very busy One day, Xiao Li of the Finance Department was recently considering a question: If each employee's salary is known, at least how many renminbi do he need to prepare in order to pay each employee without changing the employee? 
It is assumed here that the salaries of the programmers are all positive integers and unit yuan. There are six types of RMB: 100 yuan, 50 yuan, 10 yuan, 5 yuan, 2 yuan, and 1 yuan.

Input

The input data contains multiple test instances. The first line of each test instance is an integer n (n <100), which represents the number of employees, and then the salary of n employees. 
n = 0 means the end of input, no processing.

Output

For each test instance, an integer x is output, indicating at least the number of RMB sheets to be prepared. Each output occupies one line.

Sample input

3 1 2 3
0

Sample output

4

Original title link: Payroll (can be submitted)

Each time the current amount of money is smaller than it, and [closest to it] the amount of money, this is greedy thinking, until the money is reduced, the amount of money is statistically completed

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	int money[6]={100,50,10,5,2,1};
	int num;
	while(cin>>num && num!=0){
		int sar[num];
		for(int i=0;i<num;i++){
			cin>>sar[i];
		}
		int ans	= 0;
		for(int i=0;i<num;i++){
			while(sar[i]>0){
				for(int j=0;j<6;j++){
					if(money[j]<=sar[i]){
						sar[i]-=money[j];
						ans++;
						break;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

 

Published 20 original articles · won 15 · views 216

Guess you like

Origin blog.csdn.net/qq_37414463/article/details/105385437