HDU - 3348 coins
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^{''}\)
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;
}
HDU - 3348 coins