hdu 简单易懂的coins

coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2056    Accepted Submission(s): 664


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
 silu:论纸币尽量多还是尽量少 。 都可以转换为一种思路
都可以看做尽量少 ,则另一种为剩余数量的数目尽量少
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int i, t;
    scanf("%d", &t );
    int mon[6] = { 1, 5, 10, 50, 100};
    int a[6];
    while( t-- )
    {
        long long int flag1 = 0,  sum  =0;
        long long int pp, p;
        scanf("%lld", &p);
        pp = p;
        long long int k1 =0;
        long long int num[10], num2[10];
        long long int summ = 0;
        for( i = 0; i<5; i++)
            {scanf("%d", &a[i]);
            sum = sum + a[i]*mon[i];
            summ = summ+a[i];


            }
        for( i = 4; i>=0; i--)
        {
            if( pp/mon[i] < a[i])
            {
                num[k1++]  =pp/mon[i];
                pp = pp- num[k1-1]*mon[i];
            }
            else
            {
                num[k1++] = a[i];
                pp = pp -num[k1-1]*mon[i];
            }
            if( pp == 0)
            {
                flag1  = 1;
                break;
            }
        }
        if( flag1 == 0)///在这就可以 , 因为最少硬币
            ///没有结果就可以说明结果没办法
        {
            printf("-1 -1\n");
            continue;
        }
         pp = sum - p;
         long long int k2 =0;
         int flag2 = 0;
          for( i = 4; i>=0; i--)
        {
            if( pp/mon[i] < a[i])
            {
                num2[k2++] =pp/mon[i];
                pp = pp- num2[k2-1]*mon[i];
            }
            else
            {
                num2[k2++] = a[i];
                pp = pp -num2[k2-1]*mon[i];
            }
            if( pp == 0)
            {
                flag2  = 1;
                break;
            }
        }
        long long int sum1= 0, sum2  = 0;
        /*if( flag1 == 0 && flag2 == 0)
        {
            printf("-1 -1\n");
            continue;
        }*/
           for( i = 0; i<k1; i++)
           {
               sum1 =  sum1+num[i];
           }


           for( i = 0; i<k2; i++)
           {
               sum2 =  sum2+num2[i];
           }
       printf("%lld %lld\n", sum1, summ - sum2 );
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值