F

来源:互联网 发布:淘宝网11.11销售额 编辑:程序博客网 时间:2024/04/28 10:36
Total Submission(s) : 36   Accepted Submission(s) : 15
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
333 6 6 6 6 610 10 10 10 10 1011 0 1 20 20 20
 

Sample Output
6 91 10-1 -1

 

这个问题本身算是背包问题,最小硬币数量就是优先选择最大的,最大硬币数量就是优先选则最小的,一开始也是本着这个原则写的,最小算法没问题,最大的当时太想省事直接复制然后改符号了,测试数据本身没啥问题,可是在一系列wa后,我举了几个极端的例子例如100 1 1 1 1 1 然后就发现问题了。。。后来发现最大的不能通过最小的该改参数,然后,只能重写了,参照了下ppt背包问题重新写后才ac。。

代码

#include<iostream>
using namespace std;
int b[10]={0,1,5,10,50,100};
int main()
{
    int t;
    int p,r;
    int a[10],c[10],e[10];
    int i,j,k,sum;
    cin>>t;
    while(t--)
    {
        sum=0;
        cin>>p;
        r=p;
        for(i=1;i<=5;i++)
        {
           cin>>a[i];
            sum=sum+b[i]*a[i];
        }
        for(i=5;i>0;i--)
        {
            if(r/b[i]<a[i])
            {
                c[i]=r/b[i];
                r=r-b[i]*c[i];
            }
            else
            {
                c[i]=a[i];
                r=r-c[i]*b[i];
            }
        }
        if(r!=0)
        {
            cout<<"-1 -1\n";
        }else
        {
            k=sum-p;
            for(i=5;i>0;i--)
            {
                if(k/b[i]<a[i])
                {
                    e[i]=k/b[i];
                    k=k-b[i]*e[i];
                }
                else
                {
                    e[i]=a[i];
                    k=k-e[i]*b[i];
                }
            }
            if(k==0)
            {
               cout<<c[1]+c[2]+c[3]+c[4]+c[5]<<' '<<(a[1]+a[2]+a[3]+a[4]+a[5]-(e[1]+e[2]+e[3]+e[4]+e[5]))<<endl;
            }

        }

    }
}

0 0
原创粉丝点击