hdu2021(简单贪心)

来源:互联网 发布:mac word 窗口切换 编辑:程序博客网 时间:2024/06/10 08:44

题目:

发工资咯:)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 50616 Accepted Submission(s): 27616

Problem Description
作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input
输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。

Output
对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。

Sample Input
3
1 2 3
0

Sample Output
4

题意:
输入一个数n,代表有n个人,然后输入n个数,代表n个人的工资,问最少需要多少张人民币。

思路:
很简单,对每个人的工资,尽量用大面额的人民币,这样累加能得到最少的张数。

代码:

#include <iostream>int arr[6] = {100, 50, 10, 5, 2, 1};using namespace std;int check(int n){    int flag = -1;    int cnt = 0;    for(int i=0; i<6; i++){        //cout<<arr[i]<<" "<<i<<endl;        if(n>=arr[i]){            while(n&&i<6){                int temp = n%arr[i];                cnt +=(n-temp)/arr[i];                n = (n==arr[i])?0:temp;                i++;            }            flag = 1;        }        if(flag==1)break;    }    return cnt;}int main(){    int n;    while(cin>>n&&n!=0){        int tot = 0;        for(int i=0; i<n; i++){            int temp;            cin>>temp;            tot = tot + check(temp);        }        cout<<tot<<endl;    }    return 0;}
0 0
原创粉丝点击