*AcWing656《钞票和硬币》(C++)

一、题目说明

二、输入输出样例

三、思路

对于此类题目进行不断的相除和取模即可求得每种不同面值钞票的数量。但需要注意的是含有小数的钞票数值,我们再取模时不能对小数操作。所以我们可以把货币的价值和每种钞票的价值都乘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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北洋的霞洛

觉得不确可以给个鼓励小费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值