C 什么';我的代码中关于URI Online Judge中第1021题(初学者)的错误是什么?

C 什么';我的代码中关于URI Online Judge中第1021题(初学者)的错误是什么?,c,C,我为URI在线判断问题1021(初学者)编写了一个代码,但它给出了错误的答案(100%)。我不明白我的代码出了什么问题 #include <stdio.h> #include <math.h> int main() { double input; scanf("%lf",&input); int note_100= input/100; int note_50=(fmod(input,100))/50; int note_

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

#include <stdio.h>
#include <math.h>
int main()
{
    double input;
    scanf("%lf",&input);

    int note_100= input/100;
    int note_50=(fmod(input,100))/50;
    int note_20=(fmod((fmod(input,100)),50))/20;
    int note_10=(fmod((fmod((fmod(input,100)),50)),20))/10;
    int note_5=(fmod((fmod((fmod((fmod(input,100)),50)),20)),10))/5;
    int note_2=(fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5))/2;
    int note_1=(fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2))/1;
    int note50=(fmod((fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2)),1))/0.50;
    int note25=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2)),1)),0.50))/0.25;
    int note10=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2)),1)),0.50)),.25))/0.10;
    int note05=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2)),1)),0.50)),0.25)),0.10))/0.05;
    int note01=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(input,100)),50)),20)),10)),5)),2)),1)),0.50)),0.25)),0.10)),0.05))/0.01;

    printf("NOTAS:\n");
    printf("%d 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);
    printf("%d nota(s) de R$ 10.00\n",note_10);
    printf("%d nota(s) de R$ 5.00\n",note_5);
    printf("%d nota(s) de R$ 2.00\n",note_2);
    printf("MOEDAS:\n");
    printf("%d moeda(s) de R$ 1.00\n",note_1);
    printf("%d moeda(s) de R$ 0.50\n",note50);
    printf("%d moeda(s) de R$ 0.25\n",note25);
    printf("%d moeda(s) de R$ 0.10\n",note10);
    printf("%d moeda(s) de R$ 0.05\n",note05);
    printf("%d moeda(s) de R$ 0.01\n",note01);
    return 0;
}
#包括
#包括
int main()
{
双输入;
scanf(“%lf”,&input);
int note_100=输入/100;
int note_50=(fmod(输入,100))/50;
注释20=(fmod((fmod(输入,100)),50))/20;
注释10=(fmod((fmod((fmod(输入,100)),50)),20))/10;
注释5=(fmod((fmod((fmod((fmod(输入,100)),50)),20)),10))/5;
注释2=(fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5))/2;
注释1=(fmod((fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5)),2))/1;
int note50=(fmod((fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5)),2)),1))/0.50;
注释25=(fmod((fmod((fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5)),2)),1)),0.50))/0.25;
注释10=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((输入,100)),50)),20)),10)),5)),2)),1)),0.50)),0.25))/0.10;
int note05=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5)),2)),1)),0.50)),0.25)),0.10))/0.05;
int note01=(fmod((fmod((fmod((fmod((fmod((fmod((fmod((fmod(输入,100)),50)),20)),10)),5)),2)),1)),0.50)),0.25(),0.10)),0.05))/0.01;
打印F(“公证人:\n”);
printf(“%d nota(s)de R$100.00\n”,注100);
printf(“%d nota(s)de R$50.00\n”,注50);
printf(“20.00雷亚尔的%d nota(s)de R$20.00\n”,注20);
printf(“10.00雷亚尔的%d nota(s)de R$10.00\n”,注10);
printf(“5.00雷亚尔的%d nota”,注5);
printf(“2.00雷亚尔的%d公证行”,注2);
printf(“MOEDAS:\n”);
printf(“%d moeda(s)de R$1.00\n”,注1);
printf(“%d moeda(s)de R$0.50\n”,注50);
printf(“%d moeda(s)de R$0.25\n”,注25);
printf(“%d moeda(s)de R$0.10\n”,注10);
printf(“%d moeda(s)de R$0.05\n”,注05);
printf(“%d moeda(s)de R$0.01\n”,注01);
返回0;
}

问题链接:

代码正在尝试像数学一样的精确整数,其值类似于0.01,无法用典型值精确表示。这通常会导致意外的结果

更改为最小单位的整数。(按100比例)

通过在每一步减少
避免重复计算,而不是从头开始重新计算

#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;
}
#包括
#包括
内部主(空){
双输入;
scanf(“%lf”,&input);
long long cent=llround(输入*100);//按100和整数进行缩放
长音符_100=分/10000;//除以比例金额(100*100)
分%=10000;
int note_50=美分/5000;
分%=5000;
国际注20=美分/2000;
分%=2000;
...
printf(“备注:\n”);
printf(“%lld nota de R$100.00\n”,注100);
printf(“%d nota(s)de R$50.00\n”,注50);
printf(“20.00雷亚尔的%d nota(s)de R$20.00\n”,注20);
...
返回0;
}

不管是什么问题,我很确定它不需要编写这种代码。请重新思考您的方法。请阅读: