目录
题目要求
思路分析
代码实现
方法一
方法二
方法三
在这个问题中,你需要读取一个整数值并将其分解为多张钞票的和,每种面值的钞票可以使用多张,并要求所用的钞票数量尽可能少。
请你输出读取值和钞票清单。
钞票的可能面值有 100,50,20,10,5,2,1100,50,20,10,5,2,1。
经过实验证明:在本题中,优先使用面额大的钞票可以保证所用的钞票总数量最少。
输入格式
输入一个整数 NN。
输出格式
参照输出样例,输出读取数值以及每种面值的钞票的需求数量。
数据范围
0
输入样例:
576
输出样例:
576 5 nota(s) de R$ 100,00 1 nota(s) de R$ 50,00 1 nota(s) de R$ 20,00 0 nota(s) de R$ 10,00 1 nota(s) de R$ 5,00 0 nota(s) de R$ 2,00 1 nota(s) de R$ 1,00
其实是一个很简单的题目,使用求余方法即可完成。
一开始用的最笨方法写的,多个if判断,输出也是挨着打的。
#include
#include
using namespace std;
int main()
{
int m,m1;
cin>>m;
m1=m;
int a=0,b=0,c=0,d=0,e=0,f=0,g=0;
if(m>=100){
a=m/100;
m-=100*a;
}if(m>=50){
b=m/50;
m-=50*b;
}if(m>=20){
c=m/20;
m-=c*20;
}if(m>=10){
d=m/10;
m-=d*10;
}if(m>=5){
e=m/5;
m-=e*5;
}if(m>=2){
f=m/2;
m-=f*2;
}if(m>=1){
g=m/1;
m-=g;
}
cout<
用了双重循环,把金额数和对应的钞票张数分别存在两个数组里,感觉还是很麻烦 。
#include
#include
using namespace std;
int main()
{
int m,t=0,x,i;
int money[7]={100,50,20,10,5,2,1};
int cnt[7]={0};
cin>>m;
cout<=money[i]){
x=m/money[i];
m-=x*money[i];
}
cnt[t++]=x;
}
for(int i=0;i<7;i++)
cout<
后来发现没有必要用cnt数组,直接把输出拿进for循环就行。
一重循环,灵活使用求余方法,直接输出。
#include
#include
using namespace std;
int main()
{
int m,t=0,x,i;
int money[7]={100,50,20,10,5,2,1};
cin>>m;
cout<