1005 of greedy strategy

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
 
题目要求:给你一个钱数和1毛5毛10毛50毛100毛的数目,求出最大纸币兑换的数目和最小纸币兑换的数目。
思路:1最小的比较简单直接从最大到最小进行判断,尽可能多的用大面值的纸币兑换。而最大的即剩余的最小,即将剩余的钱进行开始的判断,再用总数目减去即为所求。
细节:若定义数组不要过小,否者报内存。
#include <cstdio>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
using namespace std;
int themin(int p,int ww[],int a[])
{
    int count=0,i,k;
    for(i=4;i>=0;i--)
    {
        if(p>=ww[i]*a[i])
        {
            p-=ww[i]*a[i];
            count+=ww[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 ww[],int a[])
{
    int sum[10],i,count=0;
    sum[0]=ww[0];
    for(i=1;i<=4;i++)
        sum[i]=sum[i-1]+a[i]*ww[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>ww[0])    return -1;
    else    return count+p;
}


int main()
{
    int N,i,t;
    cin>>N;
    int p,a[6]={1,5,10,50,100},ww[6];
    while(N--)
    {
        cin>>p;int sum=0;
        for(i=0;i<5;i++)
        {
            cin>>ww[i];
            sum+=ww[i]*a[i];
        }
        if(sum<p) cout<<-1<<" "<<-1<<endl;
        else
        {
            t=themin(p,ww,a);
            if(t==-1)
            {
                cout<<-1<<" "<<-1<<endl;
            }
            else
            {
                cout<<t<<" "<<themax(p,ww,a)<<endl;
            }
        }
    }
}


你可能感兴趣的:(1005 of greedy strategy)