题目1549:货币问题

题目描述:

已知有面值为1元,2元,5元,10元,20元,50元,100元的货币若干(可认为无穷多),需支付价格为x的物品,并需要恰好支付,即没有找零产生。
求,至少需要几张货币才能完成支付。
如,若支付价格为12元的物品,最少需要一张10元和一张2元,即两张货币就可完成支付。

输入:

输入包含多组测试数据,每组仅包含一个整数p(1<=p<=100000000),为需支付的物品价格。

输出:

对于每组输入数据,输出仅一个整数,代表最少需要的货币张数。

样例输入:
10
11
13
样例输出:
1
2
3
#include <cstdio>
 
using namespace std;
 
const int money[] = {100, 50, 20, 10, 5, 2, 1};
 
int main()
{
    int p;
 
    #ifndef ONLINE_JUDGE
        //freopen("d:\\OJ\\uva_in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
 
    while (scanf("%d", &p) == 1) {
        int ans = 0;
 
        for (int i = 0; i < 7; i++) {
            ans += p / money[i];
            p %= money[i];
        }
 
        printf("%d\n", ans);
    }
    return 0;
}



你可能感兴趣的:(题目1549:货币问题)