*题目 Problem F id: 1005
*题意 : 给你一个价格,还有面值分别为1,5,10,50,100(单位:毛)纸币的数量,要你用最少数量的纸币和最多数量的凑出这个价格,输出最少和最多的数量。
*解题思路 :因为我们要求的是花的最多数量纸币,所以就是要保证手上的纸币数量最少!!这样想的话问题就比较简单了,就转化为最少数量问题了。假设手上总共有p毛,而价格为q毛,我们用手上最少的数量的纸币去凑(p-q)毛,然后再用总数量减去该最少数量即可。
*感想:放了几天假,忘了写了啥
*AC源码
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
# include<cmath>
# define N 1010
# define ll long long
using namespace std;
int s,i;
int a[7];
int b[7];
int c[7]= {0,1,5,10,50,100};
int main() {
int t;
cin>>t;
while(t--) {
scanf("%d",&s);
for(int i=1; i<=5; i++) {
scanf("%d",&a[i]);
}
int x=s;
int Min=0,Max=0;
memset(b,0,sizeof b);
for( i=5; i>=1; i--) {
if(x>=c[i]) {
int num=x/c[i];
if(num>=a[i])
num=a[i];
Min+=num;
a[i]-=num;
b[i]=num;
x-=num*c[i];
}
}
if(x!=0)Min=-1;
if(Min==-1) {
printf("-1 -1\n");
continue;
}
Max=Min;
while(1) {
int flag=1;
for(int i=5; i>1; i--) {
if(b[i]) {
for(int j=i-1; j>=1; j--) {
if(b[i]==0)
break;
if(a[j]*c[j]>=c[i]) {
int x=a[j]*c[j]/c[i];
if(x>=b[i]) {
Max=Max-b[i]+c[i]*b[i]/c[j];
b[j]+=c[i]*b[i]/c[j];
a[i]+=b[i];
a[j]=a[j]-c[i]*b[i]/c[j];
b[i]=0;
flag=0;
} else {
Max=Max-x+c[i]*x/c[j];
b[j]+=c[i]*x/c[j];
a[i]+=x;
a[j]=a[j]-c[i]*x/c[j];
b[i]-=x;
flag=0;
}
}
}
}
}
if(flag)
break;
}
printf("%d %d\n",Min,Max);
}
return 0;
}
Problem F
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 161 Accepted Submission(s) : 59
"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.
3 33 6 6 6 6 6 10 10 10 10 10 10 11 0 1 20 20 20
6 9 1 10 -1 -1