2016SDAU课程练习一1005 Problem F
来源:互联网 发布:window7的com端口 编辑:程序博客网 时间:2024/06/10 10:33
Problem F
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
int min(int r,int b[],int c[],int a[]){
for(int i=5;i>0;i--){
if(r/b[i]<a[i]){
c[i]=r/b[i];
r=r-b[i]*c[i];
}
else{
c[i]=a[i];
r=r-c[i]*b[i];
}
}
return r;
}
int max(int k,int e[],int b[],int a[]){
for(int i=5;i>0;i--){
if(k/b[i]<a[i]){
e[i]=k/b[i];
k-=b[i]*e[i];
}
else{
e[i]=a[i];
k=k-e[i]*b[i];
}
}
return k;
}
int main(){
int a[7],c[7],e[7],b[6]={0,1,5,10,50,100};
int p,r,sum,k;
int t;
cin>>t;
while(t--){
sum=0;
cin>>p;
r=p;
for(int i=1;i<=5;i++){
cin>>a[i];
sum+=b[i]*a[i];
r=min(r,b,c,a);
if(r!=0){
cout<<"-1 -1"<<endl;
continue;
}
else{
k=sum-p;
k=max(k,e,b,a);
if(k==0){
printf("%d %d\n",c[1]+c[2]+c[3]+c[4]+c[5],(a[1]+a[2]+a[3]+a[4]+a[5]-(e[1]+e[2]+e[3]+e[4]+e[5])));
}
}
}
}
return 0;
}
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 100 Accepted Submission(s) : 36
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.
"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
333 6 6 6 6 610 10 10 10 10 1011 0 1 20 20 20
Sample Output
6 91 10-1 -1
又是数硬币,要求得出最大和最小硬币数,很相似之前做的第二套练习题的数硬币的题,只不过这道题要求得出最大硬币数,而这也正是本题的最大难点。
关键是转换,把最多转化为钱最多硬币最少,这一步好难好难的。。。。
#include<iostream>#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
int min(int r,int b[],int c[],int a[]){
for(int i=5;i>0;i--){
if(r/b[i]<a[i]){
c[i]=r/b[i];
r=r-b[i]*c[i];
}
else{
c[i]=a[i];
r=r-c[i]*b[i];
}
}
return r;
}
int max(int k,int e[],int b[],int a[]){
for(int i=5;i>0;i--){
if(k/b[i]<a[i]){
e[i]=k/b[i];
k-=b[i]*e[i];
}
else{
e[i]=a[i];
k=k-e[i]*b[i];
}
}
return k;
}
int main(){
int a[7],c[7],e[7],b[6]={0,1,5,10,50,100};
int p,r,sum,k;
int t;
cin>>t;
while(t--){
sum=0;
cin>>p;
r=p;
for(int i=1;i<=5;i++){
cin>>a[i];
sum+=b[i]*a[i];
r=min(r,b,c,a);
if(r!=0){
cout<<"-1 -1"<<endl;
continue;
}
else{
k=sum-p;
k=max(k,e,b,a);
if(k==0){
printf("%d %d\n",c[1]+c[2]+c[3]+c[4]+c[5],(a[1]+a[2]+a[3]+a[4]+a[5]-(e[1]+e[2]+e[3]+e[4]+e[5])));
}
}
}
}
return 0;
}
0 0
- 2016SDAU课程练习一1005 Problem F
- 2016SDAU课程练习一Problem G
- 2016SDAU课程练习一Problem E
- 2016SDAU课程练习一Problem Q
- 2016SDAU课程练习四1006 Problem F
- 课程练习一 Problem F id: 1005
- 2016SDAU课程练习一1005
- 2016SDAU课程练习一1005
- 2016SDAU课程练习一1006 Problem G
- 2016SDAU课程练习一1001 Problem B
- 2016SDAU课程练习一1012 Problem M
- 2016SDAU课程练习一1013 Problem N
- 2016SDAU课程练习一1002 Problem C
- 2016SDAU课程练习一1008 Problem I
- 课程练习一Problem F
- 2016SDAU课程练习四1005 Problem E
- 2016sdau课程练习专题一 1005 problemF
- 2016SDAU课程练习一1004
- MAC下Django环境搭建
- C++ 读取BMP文件
- 数据库设计 Step by Step (5)——理解用户需求
- OC学习之属性关键字以及set和get方法
- initramfs( initial ram filesystem) purpose
- 2016SDAU课程练习一1005 Problem F
- 今天接触vb6.0的感触
- 数据库设计 Step by Step (6) —— 提取业务规则
- Java中的反射基础知识
- 易听 - 苏教小学英语同步教材视频手机APP介绍
- EF-DataFirst切换数据库问题
- 关系数据库中关系表间的连接(内连接,外连接,左连接,右连接,全连接)
- 《C++》09 C++数据结构
- bzoj3038 上帝造题7分钟2