一、题目说明
二、输入输出样例
三、思路
对于此类题目进行不断的相除和取模即可求得每种不同面值钞票的数量。但需要注意的是含有小数的钞票数值,我们再取模时不能对小数操作。所以我们可以把货币的价值和每种钞票的价值都乘100。这样在进行取模时就不会出现问题。
四、C+题解+
//第一种写法:暴力输出
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double n;
cin >> n;
int m = n * 100;
cout << "NOTAS:" << endl;
printf("%d nota(s) de R$ 100.00\n",m/10000); m%= 10000;
printf("%d nota(s) de R$ 50.00\n",m/5000);m%= 5000;
printf("%d nota(s) de R$ 20.00\n",m/2000);m%= 2000;
printf("%d nota(s) de R$ 10.00\n",m/1000);m%= 1000;
printf("%d nota(s) de R$ 5.00\n",m/500);m%= 500;
printf("%d nota(s) de R$ 2.00\n",m/200);m%= 200;
cout << "MOEDAS:" << endl;
printf("%d moeda(s) de R$ 1.00\n",m/100);m%= 100;
printf("%d moeda(s) de R$ 0.50\n",m/50);m%= 50;
printf("%d moeda(s) de R$ 0.25\n",m/25);m%= 25;
printf("%d moeda(s) de R$ 0.10\n",m/10);m%= 10;
printf("%d moeda(s) de R$ 0.05\n",m/5);m%= 5;
printf("%d moeda(s) de R$ 0.01\n",m);
return 0;
}
//第二种写法:循环
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double n;
cin >> n;
int m = n * 100;
int a[12] = {10000,5000,2000,1000,500,200,100,50,25,10,5,1};
cout << "NOTAS:" << endl;
for(int i = 0;i<6;i++){
printf("%d nota(s) de R$ %.2lf\n",m/a[i],(double)a[i]/100);
m %= a[i];
}
cout << "MOEDAS:" << endl;
for(int i = 6;i<12;i++){
printf("%d moeda(s) de R$ %.2lf\n",m/a[i],(double)a[i]/100);
m %= a[i];
}
return 0;
}