PTA 天梯赛7-4 h0132. 钞票和硬币 (15 分) C语言AC题解

这是一个C语言程序,用于将输入的浮点数(0到1000000.00)按货币面值分解为最少数量的钞票和硬币。程序考虑了100,50,20,10,5,2面值的钞票和1,0.50,0.25,0.10,0.05,0.01面值的硬币。输入一个金额,程序会输出每种面值的钞票和硬币数量。
摘要由CSDN通过智能技术生成

读取一个带有两个小数位的浮点数,这代表货币价值。

在此之后,将该值分解为多种钞票与硬币的和,每种面值的钞票和硬币使用数量不限,要求使用的钞票和硬币的数量尽可能少。

钞票的面值是100,50,20,10,5,2。

硬币的面值是1,0.50,0.25,0.10,0.05和0.01。

输入格式:
输入一个浮点数N(0≤N≤1000000.00)。

输出格式:
参照输出样例,输出每种面值的钞票和硬币的需求数量。

输入样例:
112.03
输出样例:
NOTAS:
1 nota(s) de R$ 100.00
0 nota(s) de R$ 50.00
0 nota(s) de R$ 20.00
1 nota(s) de R$ 10.00
0 nota(s) de R$ 5.00
1 nota(s) de R$ 2.00
MOEDAS:
0 moeda(s) de R$ 1.00
0 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
0 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01

#include<stdio.h>
int main(void){
    float m;
    scanf("%f",&m);
    int i=0;
    float a[12]={100.0,50.0,20.0,10.0,5.0,2.0,1.0,0.5,0.25,0.1,0.05,0.01};
    int b[12]={0};
    int j=0;
    while(m>=a[i]){
        if (m < 0.01) {
            b[j]++;
            break;
        }
        m-=a[i];
        b[j]++;
        while(m<a[i]){
            i++;
            j++;
        }
    }
    printf("NOTAS:\n");
    for(int l=0;l<6;l++){
        printf("%d nota(s) de R$ %.2f\n",b[l],a[l]);
    }
        printf("MOEDAS:\n");
    for(int l=6;l<11;l++){
        printf("%d moeda(s) de R$ %.2f\n",b[l],a[l]);
    }
    if(b[11]>0){
        printf("%d moeda(s) de R$ %.2f\n",b[11]+1,a[11]);
    }else{
        printf("%d moeda(s) de R$ %.2f\n",b[11],a[11]);
    }
    return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值