1
I’m having an error in an algorithm I’m implementing in the c# language on the JUDGE website online, Banknotes and coins And I don’t really know what it is, but I believe it’s about the calculations, it gives a rounding error, I realized this by putting several writeln, after the calculation, just to keep up with the variations, with some inputs works perfectly. only does not work with this entry 576.43 and others. it works all right, but the last line of data output gives 2 1c coins, being that are 3 coins.. I don’t know how to explain it, but I hope you understand my logic
using System;
class MainClass {
public static void Main (string[] args) {
double n1,n2,n3,n4,n5,n6,m1,m2,m3,m4,m5,m6;
double a = double.Parse(Console.ReadLine());
n1 = (a - a % 100)/100;
a = a % 100;
n2 = (a - a % 50)/50;
a = a % 50;
n3 = (a - a % 20)/20;
a = a % 20;
n4 = (a - a % 10)/10;
a = a % 10;
n5 = (a - a % 5)/5;
a = a % 5;
n6 = (a - a % 2)/2;
a = a % 2;
m1 = (a - a % 1)/1;
a = a % 1;
m2 = (a - a % 0.50)/0.50;
a = a % 0.50;
m3 = (a - a % 0.25)/0.25;
a = a % 0.25;
m4 = (a - a % 0.10)/0.10;
a = a % 0.10;
m5 = (a - a % 0.05)/0.05;
a = a % 0.05;
m6 = (a - a % 0.01)/0.01;
Console.WriteLine("NOTAS:");
Console.WriteLine($"{n1} nota(s) de R$ 100.00");
Console.WriteLine($"{n2} nota(s) de R$ 50.00");
Console.WriteLine($"{n3} nota(s) de R$ 20.00");
Console.WriteLine($"{n4} nota(s) de R$ 10.00");
Console.WriteLine($"{n5} nota(s) de R$ 5.00");
Console.WriteLine($"{n6} nota(s) de R$ 2.00");
Console.WriteLine("MOEDAS:");
Console.WriteLine($"{m1} moeda(s) de R$ 1.00");
Console.WriteLine($"{m2} moeda(s) de R$ 0.50");
Console.WriteLine($"{m3} moeda(s) de R$ 0.25");
Console.WriteLine($"{m4} moeda(s) de R$ 0.10");
Console.WriteLine($"{m5} moeda(s) de R$ 0.05");
Console.WriteLine($"{m6} moeda(s) de R$ 0.01");
}
}
Thank you friend(a) for your reply.. but how can I use this suffix in my case? (if you are available, I would appreciate an example) is because in my program I’m doing an assignment with calculus, and this example got a little distant, but I’ll take a better look at how it uses the decimal, anyway already gave a light!
– pablo kevin
m2 = (a - a % 0.50m) / 0.50m;
 a = a % 0.50m;
etc.. But your code is too repetitive, you can use arrays and cycles to simplify / shorten..– vik
Yeah, I just haven’t learned yet.. rsrs no c# no
– pablo kevin
I really liked your solution!! was much more practical, thank you very much!!!
– pablo kevin