-1
The code gives error in cases such as 576.73 or 576.43, in the coin field of 0.01 the other field answer correctly. I don’t know exactly the reason to return 2 coins of 0.01 in the above cases, but when the input is 0.03, 0.04 or 0.06 among other attempts taken from the site uDebug the result is correct.
The description of the problem is:
Read a floating point value to two decimal places. This value represents a monetary value. Then calculate the fewest possible banknotes and coins in which the value can be broken down. The banknotes considered are 100, 50, 20, 10, 5, 2. The possible currencies are 1, 0.50, 0.25, 0.10, 0.05 and 0.01. The following shows the list of necessary notes. Entree
The input file contains a floating point value N (0 N 1000000.00). Exit
Print the minimum amount of banknotes and coins needed to exchange the initial amount, as per example provided.
Note: Use dot (.) to separate the decimal part.
Expected exit to entry 576.73
NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01
My code in C:
#include <stdio.h>
#include <stdlib.h>
int main(){
float valor;
int div;
scanf("%f", &valor);
///// NOTAS
printf("NOTAS:\n");
div = (valor/100);
valor = valor - (div*100);
printf("%d nota(s) de R$ 100.00\n", div);
div = (valor/50);
valor = valor - (div*50);
printf("%d nota(s) de R$ 50.00\n", div);
div = (valor/20);
valor = valor - (div*20);
printf("%d nota(s) de R$ 20.00\n", div);
div = (valor/10);
valor = valor - (div*10);
printf("%d nota(s) de R$ 10.00\n", div);
div = (valor/5);
valor = valor - (div*5);
printf("%d nota(s) de R$ 5.00\n", div);
div = (valor/2);
valor = valor - (div*2);
printf("%d nota(s) de R$ 2.00\n", div);
///// MOEDAS
printf("MOEDAS:\n");
valor *= 100;
div = (valor/100);
valor = valor - (div*100);
printf("%d moeda(s) de R$ 1.00\n", div);
div = (valor/50);
valor = valor - (div*50);
printf("%d moeda(s) de R$ 0.50\n", div);
div = (valor/25);
valor = valor - (div*25);
printf("%d moeda(s) de R$ 0.25\n", div);
div = (valor/10);
valor = valor - (div*10);
printf("%d moeda(s) de R$ 0.10\n", div);
div = (valor/5);
valor = valor - (div*5);
printf("%d moeda(s) de R$ 0.05\n", div);
div = valor / 1;
printf("%d moeda(s) de R$ 0.01\n", div);
return 0;
}
My output (for input: 576.73)
NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
2 moeda(s) de R$ 0.01
Thank you in advance.
Try to use the value
double
instead offloat
.– Danizavtz
It worked, even Danizavtz.
– Timafejn Yuri
Dude, I don’t know the search terms, but it is related to the representation of binary numbers in floating point (standard IEEE754 for floating point arithmetic), periodic decimation in binaries and accumulation of errors in multiplication and division, when using floating point.
– Danizavtz
To avoid the lack of precision in the float/double representations you can multiply the values by 100 and work with integers and use the rest operator of the division (%).
– anonimo