头像

胜利庄老实人




离线:1天前



题目链接 acwing 656

我遇到了当double类型数据乘以整数并赋值给整数时,出现错误的问题。

错误的代码:

#include<cstdio>

int main(){
    double n;
    scanf("%lf", &n);
    int a = n,b;
    n = n - a;
    b = a / 100;
    printf("NOTAS:\n");
    printf("%d nota(s) de R$ 100.00\n", b);
    a %= 100;
    b = a / 50;
    printf("%d nota(s) de R$ 50.00\n", b);
    a %= 50;
    b = a / 20;
    printf("%d nota(s) de R$ 20.00\n", b);
    a %= 20;
    b = a / 10;
    printf("%d nota(s) de R$ 10.00\n", b);
    a %= 10;
    b = a / 5;
    printf("%d nota(s) de R$ 5.00\n", b);
    a %= 5;
    b = a / 2;
    printf("%d nota(s) de R$ 2.00\n", b);
    a %= 2;
    b = a / 1;
    printf("MOEDAS:\n");
    printf("%d moeda(s) de R$ 1.00\n", b);
    a = 100 * n; //此处的a结果不对
    printf("%d\n", a);
    b = a / 50;
    printf("%d moeda(s) de R$ 0.50\n", b);
    a %= 50;
    b = a / 25;
    printf("%d moeda(s) de R$ 0.25\n", b);
    a %= 25;
    b = a / 10;
    printf("%d moeda(s) de R$ 0.10\n", b);
    a %= 10;
    b = a / 5;
    printf("%d moeda(s) de R$ 0.05\n", b);
    a %= 5;
    b = a / 1;
    printf("%d moeda(s) de R$ 0.01\n", b);
    return 0;
}

代码中注释处语句输出结果不为1,有没有大佬能讲解一下?




代码中注释处语句输出结果不为1,有没有大佬能讲解一下

#include<cstdio>

int main(){
    double n;
    scanf("%lf", &n);
    int a = n,b;
    n = n - a;
    b = a / 100;
    printf("NOTAS:\n");
    printf("%d nota(s) de R$ 100.00\n", b);
    a %= 100;
    b = a / 50;
    printf("%d nota(s) de R$ 50.00\n", b);
    a %= 50;
    b = a / 20;
    printf("%d nota(s) de R$ 20.00\n", b);
    a %= 20;
    b = a / 10;
    printf("%d nota(s) de R$ 10.00\n", b);
    a %= 10;
    b = a / 5;
    printf("%d nota(s) de R$ 5.00\n", b);
    a %= 5;
    b = a / 2;
    printf("%d nota(s) de R$ 2.00\n", b);
    a %= 2;
    b = a / 1;
    printf("MOEDAS:\n");
    printf("%d moeda(s) de R$ 1.00\n", b);
    a = 100 * n; //此处的a结果不对
    printf("%d\n", a);
    b = a / 50;
    printf("%d moeda(s) de R$ 0.50\n", b);
    a %= 50;
    b = a / 25;
    printf("%d moeda(s) de R$ 0.25\n", b);
    a %= 25;
    b = a / 10;
    printf("%d moeda(s) de R$ 0.10\n", b);
    a %= 10;
    b = a / 5;
    printf("%d moeda(s) de R$ 0.05\n", b);
    a %= 5;
    b = a / 1;
    printf("%d moeda(s) de R$ 0.01\n", b);
    return 0;
}