1 题目编号:1005 problemB
2 题目内容:
"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.
3 33 6 6 6 6 6 10 10 10 10 10 10 11 0 1 20 20 20
6 9 1 10 -1 -1
3 解题思路形成过程:最少的数量要用贪心的思想,优先取面值尽量大 的纸币来凑这个价格。因为我们要求的是花的最多数量纸币,所以就是要保证手上的纸币数量最少;假设手上总共有p毛,而价格为q毛,我们用手上最少的数量的纸币去凑(p-q)毛,然后再用总数量减去该最少数量即可。
5 代码:#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int b[10] = { 0, 1, 5, 10, 50, 100 };
int T;
int P, r;
int a[10], c[10], e[10];
int i, k, sum;
cin >> T;
while (T--)
{
sum = 0;
cin >> P;
r = P;
for (i = 1; i <= 5; i++)
{
cin >> a[i];
sum = sum + b[i] * a[i];
}
for (i = 5; i>0; i--)
{
if (r / b[i]<a[i])
{
c[i] = r / b[i];
r = r - b[i] * c[i];
}
else
{
c[i] = a[i];
r = r - c[i] * b[i];
}
}
if (r != 0)
{
cout << -1 << " " << -1 << endl;
}
else
{
k = sum - P;
for (i = 5; i>0; i--)
{
if (k / b[i]<a[i])
{
e[i] = k / b[i];
k = k - b[i] * e[i];
}
else
{
e[i] = a[i];
k = k - e[i] * b[i];
}
}
if (k == 0)
{
cout << (c[1] + c[2] + c[3] + c[4] + c[5]) << " " << (a[1] + a[2] + a[3] + a[4] + a[5] - (e[1] + e[2] + e[3] + e[4] + e[5]) )<< endl;
}
}
}
}