2016SDAU课程练习一_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

代码并没有AC,留着查错

#include<iostream>
#include<fstream>
#include<stdio.h>

using namespace std;
int main()
{
    ifstream cin ("nrj.txt");

    int x,money,coin,min,max,tem,s,tm,all;
    int a[5],b[5];
    cin>>x;
    while(x--)
    {
        all=0;//记录总硬币数
        s=0;//记录硬币总面值
        coin=0;//初始硬币数为0
        min=max=-1;//初始化最大和最小均为-1

        cin>>money;

        tm=money;//暂存买书需要的总钱数

        //输入各个面额的个数
        for(int i=0;i<5;i++)
        {
            cin>>a[i];
            b[i]=a[i];//备份硬币个数
            all+=a[i];
        }

        if(all==0)
        {
            printf("-1 -1\n");
            continue;
        }
        //计算需要硬币的最少个数
        for(int i=0;i<5;i++)
        {
            int m,n;
            if(i==0)        {m=100,n=4;}
            else if(i==1)   {m=50,n=3;}
            else if(i==2)   {m=10,n=2;}
            else if(i==3)   {m=5,n=1;}
            else            {m=1,n=0;}

            s+=a[n]*m;
            tem=money/m;
            if(tem<=a[n])
            {
                coin+=tem;
                money-=tem*m;
                a[n]-=tem;
            }
            else
            {
                coin+=a[n];
                money-=a[n]*m;
                a[n]=0;
            }
        }
        if(money==0)
            min=coin;
        //找出不需要买书钱最少硬币的个数,反之则为买书需要硬币最多的情况
        money=s-tm;
        coin=0;
        for(int i=0;i<5;i++)
        {    

            int m,n;
            if(i==0)        {m=100,n=4;}
            else if(i==1)   {m=50,n=3;}
            else if(i==2)   {m=10,n=2;}
            else if(i==3)   {m=5,n=1;}
            else            {m=1,n=0;}

            tem=money/m;
            if(tem<=b[n])
            {
                coin+=tem;
                money-=tem*m;
                b[n]-=tem;
            }
            else
            {
                coin+=b[n];
                money-=b[n]*m;
                b[n]=0;
            }
        }
        if(money==0)
            max=all-coin;

        printf("%d %d\n",min,max);
    }

    return 0;
}

你可能感兴趣的:(2016SDAU课程练习一_1005)