练习一1005

 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
 

Statistic | Submit | Back 
思路:
计算最少的纸币比较简单,从最大的100开始,如果p大于100,p的值减去100然后再比较若p小于100则换下一个也就是50,以此类推直至p为0.计算最大只需要计算剩余纸币最少即可。
代码:
 #include<iostream>
#include<string.h>
using namespace std;
int main()
{
int t,a[5],b[5]={1,5,10,50,100};
cin>>t;
for(int j=0;j<t;j++)
{
int p,pp,d=0,cc=0,c=0;
memset(a,0,sizeof(a));
cin>>p;
int n=p;
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=4;i>=0;i--)
{
if(p-b[i]>=0&&a[i]>=1)
for(int k=0;k<a[i]&&p-b[i]>=0;
k++)
{
p-=b[i];c++;
}
if(p==0) i=0;
if(p!=0&&i==0) c=-1;
}
for(int i=0;i<5;i++)
d+=a[i]*b[i];
for(int i=0;i<5;i++)
cc+=a[i];
pp=d-n;
for(int i=4;i>=0;i--)
{
if(pp-b[i]>=0&&a[i]>=1)
for(int k=0;k<a[i]&&pp-b[i]>=0;
k++)
{
pp-=b[i];cc--;
}
if(pp==0) i=0;
if(pp!=0&&i==0) cc=-1;
}
cout<<c<<" "<<cc<<endl;
}
return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值