Problem f
2016-03-23 19:22
211 查看
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20
[align=left]Sample Output[/align]
6 9
1 10
-1 -1
根据题意,即给出一个数,然后给出5个数,分别是1毛5毛10毛50毛100毛,来凑出前面的数,输出最少用几张,最多用几张。此题,最少好做,就用贪心算法就可,而最多就稍微麻烦一点了,请看代码注释。 思路就是, 求最少时,就用贪心算法,很简单。当求最多时,转化一下,就是求剩下钱的最少值:第一组例子,33 6 6 6 6 6,一共是996毛减去33毛,就是剩下的963毛,下面的问题 就是 963毛用现有的钱数最少用几张凑出。这样33毛不就是最对了么,反正钱的总张数是一定的。#include<iostream>
using namespace std;
int f1(int m,int a1,int a5,int a10,int a50,int a100)
{
int mm,m2=0,n,sum=0;
m2=a1+a5+a10+a50+a100;
mm=a1+5*a5+10*a10+50*a50+100*a100;
n=mm-m;
for(;n >= 100&&a100 > 0;--a100)
{
int m1=0;
++m1;
n = n-100;
sum=sum+m1;
}
for(;n >= 50&&a50 > 0;--a50)
{
int m1=0;
++m1;
n = n-50;
sum=sum+m1;
}
for(;n >= 10&&a10 > 0;--a10)
{
int m1=0;
++m1;
n = n-10;
sum=sum+m1;
}
for(;n >= 5&&a5 > 0;--a5)
{
int m1=0;
++m1;
n = n-5;
sum=sum+m1;
}
for(;n >= 1&&a1 > 0;--a1)
{
int m1=0;
n = n-1;
++m1;
sum=sum+m1;
}
if(n == 0)
return (m2-sum);
else
return -1;
}
int f2(int n,int a1,int a5,int a10,int a50,int a100)
{
int sum=0;
for(;n >= 100&&a100 > 0;--a100)
{
int m1=0;
++m1;
n = n-100;
sum=sum+m1;
}
for(;n >= 50&&a50 > 0;--a50)
{
int m1=0;
++m1;
n = n-50;
sum=sum+m1;
}
for(;n >= 10&&a10 > 0;--a10)
{
int m1=0;
++m1;
n = n-10;
sum=sum+m1;
}
for(;n >= 5&&a5 > 0;--a5)
{
int m1=0;
++m1;
n = n-5;
sum=sum+m1;
}
for(;n >= 1&&a1 > 0;--a1)
{
int m1=0;
n = n-1;
++m1;
sum=sum+m1;
}
if(n == 0)
return sum;
else
return -1;
}
int main()
{
int m,k,Min,Max;
int n,m1=0,m2,a1,a5,a10,a50,a100;
cin>>k;
while(k)
{
--k;
cin>>n>>a1>>a5>>a10>>a50>>a100;
Min = f2(n,a1,a5,a10,a50,a100);
Max = f1(n,a1,a5,a10,a50,a100);
cout<<Min<<" "<<Max<<endl;
}
}
"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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20
[align=left]Sample Output[/align]
6 9
1 10
-1 -1
根据题意,即给出一个数,然后给出5个数,分别是1毛5毛10毛50毛100毛,来凑出前面的数,输出最少用几张,最多用几张。此题,最少好做,就用贪心算法就可,而最多就稍微麻烦一点了,请看代码注释。 思路就是, 求最少时,就用贪心算法,很简单。当求最多时,转化一下,就是求剩下钱的最少值:第一组例子,33 6 6 6 6 6,一共是996毛减去33毛,就是剩下的963毛,下面的问题 就是 963毛用现有的钱数最少用几张凑出。这样33毛不就是最对了么,反正钱的总张数是一定的。#include<iostream>
using namespace std;
int f1(int m,int a1,int a5,int a10,int a50,int a100)
{
int mm,m2=0,n,sum=0;
m2=a1+a5+a10+a50+a100;
mm=a1+5*a5+10*a10+50*a50+100*a100;
n=mm-m;
for(;n >= 100&&a100 > 0;--a100)
{
int m1=0;
++m1;
n = n-100;
sum=sum+m1;
}
for(;n >= 50&&a50 > 0;--a50)
{
int m1=0;
++m1;
n = n-50;
sum=sum+m1;
}
for(;n >= 10&&a10 > 0;--a10)
{
int m1=0;
++m1;
n = n-10;
sum=sum+m1;
}
for(;n >= 5&&a5 > 0;--a5)
{
int m1=0;
++m1;
n = n-5;
sum=sum+m1;
}
for(;n >= 1&&a1 > 0;--a1)
{
int m1=0;
n = n-1;
++m1;
sum=sum+m1;
}
if(n == 0)
return (m2-sum);
else
return -1;
}
int f2(int n,int a1,int a5,int a10,int a50,int a100)
{
int sum=0;
for(;n >= 100&&a100 > 0;--a100)
{
int m1=0;
++m1;
n = n-100;
sum=sum+m1;
}
for(;n >= 50&&a50 > 0;--a50)
{
int m1=0;
++m1;
n = n-50;
sum=sum+m1;
}
for(;n >= 10&&a10 > 0;--a10)
{
int m1=0;
++m1;
n = n-10;
sum=sum+m1;
}
for(;n >= 5&&a5 > 0;--a5)
{
int m1=0;
++m1;
n = n-5;
sum=sum+m1;
}
for(;n >= 1&&a1 > 0;--a1)
{
int m1=0;
n = n-1;
++m1;
sum=sum+m1;
}
if(n == 0)
return sum;
else
return -1;
}
int main()
{
int m,k,Min,Max;
int n,m1=0,m2,a1,a5,a10,a50,a100;
cin>>k;
while(k)
{
--k;
cin>>n>>a1>>a5>>a10>>a50>>a100;
Min = f2(n,a1,a5,a10,a50,a100);
Max = f1(n,a1,a5,a10,a50,a100);
cout<<Min<<" "<<Max<<endl;
}
}
相关文章推荐
- ajax实现jsp页面的动态刷新时间
- C++ 穷举法 三色球问题
- 02Linux文件系统基本结构
- HDU2028JAVA
- openal-1.13 静态编译(mingw32)
- lintcode:最小编辑距离
- git status遇到old mode问题
- iOS常用的第三方框架
- 多态
- PHP中检索字符串
- CentOS安装JDK8
- 第四周上机实践项目——项目5-用递归方法求解
- 对理想团队模式构建的设想以及对软件流程的理解
- MySQL索引背后的数据结构及算法原理
- Eclipse 新建Dynamic Web Project时, 项目默认的输出目录为build\classes,如何将Web项目的默认编译输出目录改为WebContent/WEB-INF/classe
- Qt 调用 Java 方法笔记
- PS切图小技巧
- 01Linux_BASH基本操作
- 友元类,友元函数
- Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy22]