1:问题描述
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.
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
2:大致题意
找钱问题,分别输入1角,5角,10角,50角,100角的硬币数量,凑够要找的钱数,分别求出最多的硬币数和最少的硬币数。
3:思路
最少的硬币数直接从最大的开始检索,如果需要的钱数大于最大的钱数,
那么就把最大的这个全用上,在继续检索下一个较小的硬币。
最少的比较好求,那么最多的怎么求那?
我们可以用总的钱数减去要找的钱数,那么就是手里还剩的钱数,总的硬币数是一样的,要使找出去的钱最多,可以让手里剩的钱最少就可以了。
4:感想
化归的思想,将一个没有解决的问题转化为已经解决的的问题或者我们比较熟悉的问题。
5:ac代码
#include <cstdio>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
using namespace std;
int themin(int p,int ww[],int a[])
{
int count=0,i,k;
for(i=4;i>=0;i--)
{
if(p>=ww[i]*a[i])
{
p-=ww[i]*a[i];
count+=ww[i];
}
else
{
k=p/a[i];
count+=k;
p-=k*a[i];
}
}
if(p>0) return -1;
else return count;
}
int themax(int p,int ww[],int a[])
{
int sum[10],i,count=0;
sum[0]=ww[0];
for(i=1;i<=4;i++)
sum[i]=sum[i-1]+a[i]*ww[i];
for(i=4;i>0;i--)
{
if(p<=sum[i-1]) continue;
else
{
int t;
t=((p-sum[i-1])/a[i])+(((p-sum[i-1])%a[i])?1:0);
count+=t;
p-=t*a[i];
}
}
if(p>ww[0]) return -1;
else return count+p;
}
int main()
{
int N,i,t;
cin>>N;
int p,a[6]={1,5,10,50,100},ww[6];
while(N--)
{
cin>>p;int sum=0;
for(i=0;i<5;i++)
{
cin>>ww[i];
sum+=ww[i]*a[i];
}
if(sum<p) cout<<-1<<" "<<-1<<endl;
else
{
t=themin(p,ww,a);
if(t==-1)
{
cout<<-1<<" "<<-1<<endl;
}
else
{
cout<<t<<" "<<themax(p,ww,a)<<endl;
}
}
}
}