编号:Problem F
Problem 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.
"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
题目大意:
有一定面额和数量的纸币去买一定价格的书,求用最少张纸币和最多张纸币的数量。
思路:
最初的想法是把所有的可能性都列出来,但这种方法会花费大量的时间,于是就放弃了,之后参考了姜的法方,用两个函数,分别计算最大值和最小值,首先算出拥有的所有的钱的总和,如果小于书的价格,则结束;若不小于,再使用贪心计算最小值,先从面值最大的纸币入手,算出最大纸币面值的总和a,如果面值总和小于书的价格v,那v等于v减去a,在计算第二大面值的总和b,然后循环下一步;如果a大于v,那么使用能够使用的最大纸币的最大张数b,再去计算下一步。对于最大数量,姜用的算法太妙了,计算最大数量不就是计算所有纸币的价值总和减去数的价格所花费的最小数量的纸币吗?然后剩下的算法与最小值的算法就一样了!
感想:
拖延症越来越厉害了!!!都快拖了一个星期了;
代码:
# include <iostream>
using namespace std;
int min (int a[], int b[], int p)
{
int k, count = 0;
for(int i = 4; i >= 0; i --)
{
if (p >= a[i]*b[i])
{
count += b[i];
p -= a[i]*b[i];
}
else
{
k = p / a[i];
count += k;
p -= a[i]*k;
}
}
if (p > 0)
return -1;
return count;
{
int k, count = 0;
for(int i = 4; i >= 0; i --)
{
if (p >= a[i]*b[i])
{
count += b[i];
p -= a[i]*b[i];
}
else
{
k = p / a[i];
count += k;
p -= a[i]*k;
}
}
if (p > 0)
return -1;
return count;
};
int max (int a[], int b[], int p, int sum)
{
int q = sum - p, count = 0, k, qq = 0;
for(int i = 0; i < 5; i ++)
{
qq += b[i];
}
int max (int a[], int b[], int p, int sum)
{
int q = sum - p, count = 0, k, qq = 0;
for(int i = 0; i < 5; i ++)
{
qq += b[i];
}
for(int i = 4; i >= 0; i --)
{
if (q >= a[i]*b[i])
{
count += b[i];
q -= a[i]*b[i];
}
else
{
k = q / a[i];
count += k;
q -= a[i]*k;
}
}
if (q > 0)
return -1;
return qq- count ;
};
{
if (q >= a[i]*b[i])
{
count += b[i];
q -= a[i]*b[i];
}
else
{
k = q / a[i];
count += k;
q -= a[i]*k;
}
}
if (q > 0)
return -1;
return qq- count ;
};
int main()
{
int n, p, a[5]={1, 5, 10, 50, 100}, b[5];
cin >> n;
while (n --)
{
int count1 = 0, flag = 1;;
int sum = 0;
cin >> p;
for(int i = 0; i < 5; i++)
{
cin >> b[i];
sum += a[i]*b[i];
}
if (sum < p)
cout << -1 << " " << -1<<endl;
else
{
cout << min(a, b, p) << " ";
cout << max(a, b, p, sum) <<endl;
}
}
return 0;
}