您的位置:首页 > 其它

Problem f

2016-03-23 19:22 211 查看
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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".

[align=left]Sample Input[/align]

3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20

[align=left]Sample Output[/align]

6 9
1 10
-1 -1
根据题意,即给出一个数,然后给出5个数,分别是1毛5毛10毛50毛100毛,来凑出前面的数,输出最少用几张,最多用几张。此题,最少好做,就用贪心算法就可,而最多就稍微麻烦一点了,请看代码注释。 思路就是, 求最少时,就用贪心算法,很简单。当求最多时,转化一下,就是求剩下钱的最少值:第一组例子,33 6 6 6 6 6,一共是996毛减去33毛,就是剩下的963毛,下面的问题 就是 963毛用现有的钱数最少用几张凑出。这样33毛不就是最对了么,反正钱的总张数是一定的。#include<iostream>
using namespace std;
int f1(int m,int a1,int a5,int a10,int a50,int a100)
{
    int mm,m2=0,n,sum=0;
    m2=a1+a5+a10+a50+a100;
    mm=a1+5*a5+10*a10+50*a50+100*a100;
    n=mm-m;

    for(;n >= 100&&a100 > 0;--a100)
    {
         int m1=0;
          ++m1;
        n = n-100;
        sum=sum+m1;
    }
    for(;n >= 50&&a50 > 0;--a50)
    {
          int m1=0;
          ++m1;
        n = n-50;
        sum=sum+m1;
    }
    for(;n >= 10&&a10 > 0;--a10)
    {
          int m1=0;
          ++m1;
        n = n-10;
        sum=sum+m1;
    }
    for(;n >= 5&&a5 > 0;--a5)
    {
         int m1=0;
          ++m1;
        n = n-5;
        sum=sum+m1;
    }
    for(;n >= 1&&a1 > 0;--a1)
    {
         int m1=0;
        n = n-1;
        ++m1;
         sum=sum+m1;
    }
    if(n == 0)
        return (m2-sum);
    else
        return -1;

}
int f2(int n,int a1,int a5,int a10,int a50,int a100)
{
    int sum=0;
   for(;n >= 100&&a100 > 0;--a100)
    {
         int m1=0;
          ++m1;
        n = n-100;
        sum=sum+m1;
    }
    for(;n >= 50&&a50 > 0;--a50)
    {
        int m1=0;
          ++m1;
        n = n-50;
        sum=sum+m1;
    }
    for(;n >= 10&&a10 > 0;--a10)
    {
        int m1=0;
          ++m1;
        n = n-10;
        sum=sum+m1;
    }
    for(;n >= 5&&a5 > 0;--a5)
    {
         int m1=0;
          ++m1;
        n = n-5;
        sum=sum+m1;
    }
    for(;n >= 1&&a1 > 0;--a1)
    {
         int m1=0;
        n = n-1;
        ++m1;
        sum=sum+m1;
    }
    if(n == 0)
        return sum;
    else
        return -1;

}
int main()
{
    int m,k,Min,Max;
    int n,m1=0,m2,a1,a5,a10,a50,a100;
    cin>>k;
    while(k)
    {
        --k;
    cin>>n>>a1>>a5>>a10>>a50>>a100;
    Min = f2(n,a1,a5,a10,a50,a100);
    Max = f1(n,a1,a5,a10,a50,a100);
    cout<<Min<<" "<<Max<<endl;
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: