HDU-3348 coins
Description
"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn't like to get the change, that is, he will give the bookseller exactly P Jiao.
Input
T(T<=100) in the first line, indicating the case number.
T lines with 6 integers each:
P a1 a5 a10 a50 a100
ai means number of i-Jiao banknotes.
All integers are smaller than 1000000.
Output
Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can't buy the book with no change, output "-1 -1".
Sample Input
3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20
Sample Output
6 9
1 10
-1 -1
题意
给你一些1角,5角,10角,50角,100角的硬币,数量不等,有一个要凑的钱数P,用硬币去凑出P
问所用硬币的最少数量和最多数量分别是多少,如果不能凑出输出-1 -1
题解
最少数量贪心比较容易想,我们优先用面额大的去凑即可,最多数量直接贪心不好搞,我们需要转化一下思路,我们把手上的钱所能凑出的总钱数计算出来,用总钱数减去要凑的钱数得到一个钱数(P^{''})
这样用最少的钱数凑出(P)即为用最多的钱数凑出(P^{''}),然后再用钱的总数量减去这个数量即为最少数量的钱币凑出(P)
AC代码
#include<iostream>
#include<stdio.h>
using namespace std;
int p, a1, a5, a10, a50, a100;
int calc(int x) {
int num100 = x / 100 < a100 ? x / 100 : a100;
x -= 100 * num100;
int num50 = x / 50 < a50 ? x / 50 : a50;
x -= 50 * num50;
int num10 = x / 10 < a10 ? x / 10 : a10;
x -= 10 * num10;
int num5 = x / 5 < a5 ? x / 5 : a5;
x -= 5 * num5;
int num1 = x;
if (num1 > a1) return -1;
else return num1 + num5 + num10 + num50 + num100;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d%d%d", &p, &a1, &a5, &a10, &a50, &a100);
int sum = a1 + a5 * 5 + a10 * 10 + a50 * 50 + a100 * 100;
int sum1 = a1 + a5 + a10 + a50 + a100;
if (calc(p) == -1) cout << "-1 -1" << endl;
else {
cout << calc(p) << " ";
cout << sum1 - calc(sum - p) << endl;
}
}
return 0;
}