HDU 3348 贪心

 http://acm.hdu.edu.cn/showproblem.php?pid=3348

"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

题目大意:分别给出你所拥有的1角、5角、10角、50角、100角的纸币数量和要买的东西的价钱,求购买这件物品所需的最少纸币数量和最大纸币数量。(所花纸币价格加起来必须刚好等于东西的价格)

思路:最少纸币数量很好求,每次都用当前最大面额的纸币进行判断。最大的我们采取逆向思维来做,购买物品消耗了最大纸币数量=手中剩下了最小纸币数量,问题转化为求购买价格为sum-value的商品所花费的最小纸币数量。(sum为自己拥有的总钱数,value为商品价格)

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;

int a[5];
int p[5]={1,5,10,50,100};

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int sum;
		scanf("%d",&sum);
		int temp=sum;
		for(int i=0;i<5;i++)
			scanf("%d",&a[i]);
		int c1=0,c2=0;
		for(int i=4;i>=0;i--)//买书 最少纸币数量
		{
			if(p[i]>sum||a[i]==0)
				continue;
			else
			{
				int n=sum/p[i];
				int n2=min(n,a[i]);
				c1+=n2;
				sum-=n2*p[i];
			}
		}
		if(sum!=0)
			printf("-1 -1\n");
		else	//总钱数-书钱 最少纸币数量
		{
			for(int i=0;i<=4;i++)
				sum+=a[i]*p[i];
			sum-=temp;
			for(int i=4;i>=0;i--)
			{
				if(p[i]>sum||a[i]==0)
					continue;
				else
				{
					int n=sum/p[i];
					int n2=min(n,a[i]);
					c2+=n2;
					sum-=n2*p[i];
				}
			}
			printf("%d %d\n",c1,a[0]+a[1]+a[2]+a[3]+a[4]-c2);
		}
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值