关于URI Online Judge中编号为1021(初学者)的问题,我的代码有什么问题?

时间:2019-01-25 16:43:40

标签: c

我为URI在线判断问题1021(初学者)写了一个代码,但回答错误(100%)。我不明白我的代码有什么问题!

1

问题链接:https://www.urionlinejudge.com.br/judge/en/problems/view/1021

1 个答案:

答案 0 :(得分:2)

代码正在尝试使用像0.01这样的值无法用典型的double来精确表示像数学这样的精确整数。这通常会导致意外的结果。

更改为最小单位的整数。 (按100缩放)

通过在每个步骤上减少cent来避免重复计算,而不是从头开始重新计算。

#include <math.h>
#include <stdio.h>

int main(void) {
    double input;
    scanf("%lf",&input);
    long long cent = llround(input * 100);  // scale by 100 and round

    long long note_100 = cent/10000;  // divide by scaled amount (100*100)
    cent %= 10000;
    int note_50 = cent/5000;
    cent %= 5000;
    int note_20 = cent/2000;
    cent %= 2000;
    ...
    printf("NOTAS:\n");
    printf("%lld nota(s) de R$ 100.00\n", note_100);
    printf("%d nota(s) de R$ 50.00\n", note_50);
    printf("%d nota(s) de R$ 20.00\n", note_20);
    ...
    return 0;
}