我现在做的是编号为1005的试题,内容如下:
Problem F
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 63 Accepted Submission(s) : 27
"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
简单题意:
mw有很多零钱,包括1角的,五角的,一元的,五元的和十元的。一天他要买东西,计算最少他要用的零钱张数和最多使用零钱的张数。
解题思路:
先计算使用零钱张数最少的情况,显然,要想使用的最少,先判断价格用10元的话需要几张,将所需张数放到一个数组中,再把总价减去这几张的金额,再判断这些钱需要几张5元的,依此类推,直到最后的金额为零,如果到最后不为零,则不能进行购买,最后把这个数组进行相加,得出最少的所需纸币的张数,同样的道理,要想花费的张数最多,那么就是剩下的最少,计算剩下的金额张数即可,与上面的道理一样。得出张数之后,再与总张数相减即可。
编写代码:
#include <iostream>
using namespace std;
int b[10] = {0, 1, 5, 10, 50, 100};
int sortPrice(int price, int a[10], int b[10], int c[10])
{
for (int i=5; i>0; i--)
{
if (price / b[i] < a[i])
{
c[i] = price / b[i];
price -= c[i] * b[i];
}
else
{
c[i] = a[i];
price -= c[i] * b[i];
}
}
return price;
}
int main()
{
int t, k, sum;
cin >> t;
int a[10], c[10], e[10];
while(t--)
{
sum = 0;
int price;
cin >> price;
int charge;
charge = price;
for (int i=1; i<= 5; i++)
{
cin >> a[i];
sum += b[i] * a[i];
}
charge = sortPrice(charge, a, b, c);
if (charge != 0)
{
cout << "-1" << " " << "-1" << endl;
}
else
{
k = sum - price;
k = sortPrice(k, a, b, e);
if (k == 0)
{
cout << c[1] + c[2] + c[3] + c[4] + c[5] << " ";
cout << a[1] + a[2] + a[3] + a[4] + a[5] - e[1] - e[2] - e[3] - e[4] - e[5] << endl;
}
}
}
}