课程练习一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)
"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

 

题意: 就是输入T,然后T行;每行输入一个P然后输入5个数,第一个数代表1 的个数,第二个数代表5的个数,第三个数,代表10 的个数,

第四个数代表50的个数,第五个数代表100 的个数。问怎样怎样组合才能使得这些数 的和==P,(可以不全使用),输出其使用的数的最小

和最大个数。


思路:

求最小的数的话,直接几个判断语句 就ok了,

求最大的数的话,第一想法是dfs,然后取出最大的情况。(其实可以直接用 dfs就可以取出最大和最小的情况)

(提交了一次,超时了,现附上超时 时的代码)

既然超时思路肯定不对,没办法,做题时想法总是认定一种接发后,很难想到其他的做法,那就问百度,c a o,

反过来想不就行吗,取最小的数的时候参数是P,取最大数的时候参数是sum-P (sum是所有数的综合).

就是逆向思维接,正确代码在下面: 



代码:(提交超时)

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
//int num=0;
int k=0;
int b[100001];


int min_number(int b[],int P,int n)
{
while (P > 0)
{
if (P >= 100)
{
if (b[4] > 0)
{
b[4]--;
P -= 100;
n++;
}
}
else if (P >= 50)
{
if (b[3] > 0)
{
b[3]--;
P -= 50;
n++;
}
}
else if (P >= 10)
{
if (b[2] > 0)
{
b[2]--;
P -= 10;
n++;
}
}
else if (P >= 5)
{
if (b[1] > 0)
{
b[1]--;
P -= 5;
n++;
}
}
else if (P >= 1)
{
if (b[0] > 0)
{
b[0]--;
P -= 1;
n++;
}
else {
break;
}
}
}
if (P==0)
return n;
else return -1;
}
void max_number(int b0,int b1,int b2,int b3,int b4,int P,int num) //dfs取出可能的情况(由于数据太大,超时了)
{
      if(P<0)
      return;
      if(P==0)
      {
      b[k++]=num;
      }
      if(b0>0)
      {
      max_number(b0-1,b1,b2,b3,b4,P-1,num+1);
      }      
      if(b1>0)
      {
      max_number(b0,b1-1,b2,b3,b4,P-5,num+1);
      }
      if(b2>0)
      {
      max_number(b0,b1,b2-1,b3,b4,P-10,num+1);
      }      
      if(b3>0)
      {
      max_number(b0,b1,b2,b3-1,b4,P-50,num+1);
      }
      if(b4>0)
      {
      max_number(b0,b1,b2,b3,b4-1,P-100,num+1);
      }      
}


int main()
{
int T;
cin >> T;
for (int i = 0; i < T; i++)
{
int P;
cin >> P;
//num=0;
int a[5];
k=0;
   memset(b,0,sizeof(b));
for (int i = 0; i < 5; i++)
{
cin >> a[i];
}
max_number(a[0],a[1],a[2],a[3],a[4],P,0);
//sort(b,b+k,cmp);
if(b[0]==0)
b[0]=-1;
cout << min_number(a,P,0) <<" "<<b[0]<< endl;
}
system("pause");
return 0;
}


正确代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;


int min_max_number(int b[],int P,int n)
{
while (P > 0)
{
if (P >= 100&&b[4]>0)
        {
b[4]--;
P -= 100;
n++;
}
else if (P >= 50&&b[3]>0)
{
b[3]--;
P -= 50;
n++;
}
else if (P >= 10&&b[2]>0)
{
b[2]--;
P -= 10;
n++;
}
else if (P >= 5&&b[1]>0)
{
b[1]--;
P -= 5;
n++;
}
else if (P >= 1)
{
if (b[0] > 0)
{
b[0]--;
P -= 1;
n++;
}
else {
break;
}
}
}
if (P==0)
return n;
else return -1;
}
int main()
{
int T;
cin >> T;
for (int i = 0; i < T; i++)
{
int P;
cin >> P;
int a[5],b[5];
int sum=0,sum1=0;
for (int i = 0; i < 5; i++)
{
cin >> a[i];
b[i]=a[i];
sum1+=a[i];
}
sum=a[0]+a[1]*5+a[2]*10+a[3]*50+a[4]*100;
cout << min_max_number(a,P,0) <<" ";
int cou=min_max_number(b,sum-P,0);
if(cou==-1)
cout<<cou<<endl;
else
cout<<sum1-cou<<endl;
}
system("pause");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值