1. 程式人生 > >SDAU課程練習1005

SDAU課程練習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 題目大意: 現在有 一本書 33 元(角), 現有 一元 6 個5元 6個,十元 6個,五十元 6個,一百元 6個。 問,最少需要幾個鈔票湊夠 33,最多呢? 思路: 求最少的時候,從大到小檢索,儘可能用更多的大面額,這裡還是比較簡單的。可能求最多的時候有些困難,但是!!我們完全可以轉化一下! 如果說 用的鈔票最多的話,那麼就是手裡剩餘鈔票最少!!那麼我們完全可以用剛才寫的求最少的函式,求最多鈔票數量!!對不對?? 感想:

這個想法是姜赫同學咆哮著告訴我的,當時情況是這樣的。 姜赫同學:“我真的是太機智了!!有沒有?!有沒有 ?哎,你就告訴我,有沒有??有沒有很機智哎?!” 在一個只有我們三個的自習室。。(還好只有我們三個,。)  AC程式碼: #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int themin(int p,int arr[],int a[])
{
    int count=0,i,k;
    for(i=4;i>=0;i--)
    {
        if(p>=arr[i]*a[i])
        {
            p-=arr[i]*a[i];
            count+=arr[i];
        }
        else
        {
            k=p/a[i];
            count+=k;
            p-=k*a[i];
        }
    }
    if(p>0) return -1;
    else return count;
}
int themax(int p,int arr[],int a[])
{
    int sum[10],i,count=0;
    sum[0]=arr[0];
    for(i=1;i<=4;i++)
        sum[i]=sum[i-1]+a[i]*arr[i];     for(i=4;i>0;i--)
    {
        if(p<=sum[i-1]) continue;
        else
        {
            int t;
            t=((p-sum[i-1])/a[i])+(((p-sum[i-1])%a[i])?1:0);
            count+=t;
            p-=t*a[i];
        }
    }
    if(p>arr[0])    return -1;
    else    return count+p;
}
int main()
{
    //freopen("r.txt", "r", stdin);
    int N,i,t;
    cin>>N;
    int p,a[6]={1,5,10,50,100},arr[6];
    while(N--)
    {
        cin>>p;int sum=0;
        for(i=0;i<5;i++)
        {
            cin>>arr[i];
            sum+=arr[i]*a[i];
        }
        if(sum

<<-1<<" "<<-1<<endl;
        else
        {
            t=themin(p,arr,a);
            if(t==-1)
            {
                cout<<-1<<" "<<-1<<endl;
            }
            else
            {
                cout<<t<<" "<<themax(p,arr,a)<<endl;
            }
        }
    }
}