-4

I write a code for the URI online judge problem no 1021(Beginner) but it says Wrong answer(100%). I can't understand what's wrong in my code!

#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;
}

Problem link: https://www.urionlinejudge.com.br/judge/en/problems/view/1021

  • 8
    Whatever the problem is, I am pretty sure it won't require writing *this* kind of code. Please rethink your approach. – Eugene Sh. Jan 25 '19 at 16:45
  • Please read: [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Weather Vane Jan 25 '19 at 17:48

1 Answers1

2

Code is attempting exact integer like math with values like 0.01 that cannot be exactly represented with typical double. This often leads to unexpected results.

Change to integers of smallest unit. (Scale by 100)

Avoid repeated calculations by reducing cent at each step rather than recalculate from the beginning.

#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;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256