1005 banknotes

1005 Problem F

"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)

题意:对于给定价格的一本书,有大小不同面值的纸币,在不找零的情况下,所需要的纸币数量最少和最多分别是多少。

思路:在求解过程中,纸币最少则大面额的数量尽可能多,而纸币最多则小面额的数量尽可能多。

感想:需要逐层分析思考,在求解中,每一面值的纸币数量要多少,在这个问题上纠结了很长时间!

#include<iostream>

#include<string.h>

#include<algorithm>

#include<stdio.h>

using namespace std;

int min(int a[],int num[],int p,int s[]){

   int i,ans=0;

   for(i=5;i>1;i--){

       if(p>=num[i]*a[i]){

           ans+=num[i];

           p-=num[i]*a[i];

       }

       else{

           ans+=p/a[i];

           p%=a[i];

       }

    }

   if(p>num[1]) return -1;

   else  return ans+p;

}

int max(int a[],int num[],int p,int s[]){

   int i,ans=0;

   for(i=5;i>1;i--){

       if(p<=s[i-1]) continue;

       else{

           int k,j;

           j=(p-s[i-1])%a[i];

           if(j>0)

            k=((p-s[i-1])/a[i])+1;

           else   k=((p-s[i-1])/a[i]);

           ans+=k;

           p-=k*a[i];

       }

    }

   if(p>num[1]) return -1;

   else return ans+p;

}

void cm(int a[],int num[],int p)

{

   int i;

   int s[6]={0};

   s[1]=num[1];

       for(i=2;i<=5;i++)

           s[i]=s[i-1]+a[i]*num[i];

       int mmin,mmax;

       mmin=min(a,num,p,s);

       if(mmin==-1)  cout<<-1<<" "<<-1<<endl;

       else{

           mmax=max(a,num,p,s);

           if(mmax==-1)   cout<<-1<<""<<-1<<endl;

           else

                cout<<mmin<<""<<mmax<<endl;

    }

}

int main(){

   int sum,i,p,t;

   int a[6]={0,1,5,10,50,100};

   int num[6];

   cin>>t;

   while(t--){

       cin>>p;

       sum=0;

       for(i=1;i<6;i++){

                cin>>num[i];

                sum+=num[i]*a[i];

       }

       if(sum<p) cout<<-1<<" "<<-1<<endl;

       else  cm(a,num,p);

       }

       return 0;

}

你可能感兴趣的:(1005 banknotes)