C#分钱计划

时间:2015-11-06 00:24:43

标签: c#

double a, ostanek;
a=Convert.ToDouble(Console.ReadLine()); 
int apoen_500, apoen_200, apoen_100, apoen_50, apoen_20, apoen_10, apoen_5, apoen2, apoen1;
int ost50cent, ost20cent, ost10cent, ost5cent, ost2cent, ost1cent;
apoen_500 = (int)a / 500;
ostanek = a%500;

apoen_200=(int)ostanek/200;
ostanek = ostanek % 200;

apoen_100 = (int)ostanek / 100;
ostanek = ostanek % 100;

apoen_50 = (int)ostanek / 50;
ostanek = ostanek % 50;
apoen_20 = (int)ostanek / 20;
ostanek = ostanek % 20;
apoen_10 = (int)ostanek / 10;
ostanek = ostanek % 10;
apoen_5 = (int)ostanek /5 ;
ostanek = ostanek % 5;
apoen2 = (int)ostanek / 2;
ostanek = ostanek % 2;
apoen1 = (int)ostanek / 1;
ostanek = ostanek % 1;

ost50cent = (int)(ostanek / 0.50);
ostanek = ostanek % 0.50;
ost20cent = (int)(ostanek / 0.20);
ostanek = ostanek % 0.20;
ost10cent = (int)(ostanek / 0.10);
ostanek = ostanek % 0.10;
ost5cent = (int)(ostanek / 0.05);
ostanek = ostanek % 0.05;
ost2cent = (int)(ostanek / 0.02);
ostanek = ostanek % 0.02;
ost1cent = (int)(ostanek / 0.01);
ostanek = ostanek % 0.01;

Console.WriteLine(apoen_500 +"x500");
Console.WriteLine(apoen_200 + "x200");
Console.WriteLine(apoen_100 + "x100");
Console.WriteLine(apoen_50 + "x50");
Console.WriteLine(apoen_20 + "x20");    
Console.WriteLine(apoen_10 + "x10");    
Console.WriteLine(apoen_5 + "x5");
Console.WriteLine(apoen2 + "x2");
Console.WriteLine(apoen1 + "x1");
Console.WriteLine(ost50cent + "x50 centov");
Console.WriteLine(ost20cent + "x20 centov");
Console.WriteLine(ost10cent + "x10 centov");
Console.WriteLine(ost5cent + "x5 centov");
Console.WriteLine(ost2cent + "x2 centov");
Console.WriteLine(ost1cent + "x1 centov");

所以我有这个代码,但它没有按计划运行。它倾向于将钱分成小块,但它不适用于50,20,10,5,21美分。它无法正确显示。就像我输入90,75一样,它显示1x50€1x 20€1x50c 1x20c 2x 2美分,但应该是1x5美分。

1 个答案:

答案 0 :(得分:1)

您应该使用decimal进行货币计算。 double用于科学计算。

更改这些行:

decimal a, ostanek;
a = Convert.ToDecimal(Console.ReadLine());

ost50cent = (int)(ostanek / 0.50m);
ostanek = ostanek % 0.50m;
ost20cent = (int)(ostanek / 0.20m);
ostanek = ostanek % 0.20m;
ost10cent = (int)(ostanek / 0.10m);
ostanek = ostanek % 0.10m;
ost5cent = (int)(ostanek / 0.05m);
ostanek = ostanek % 0.05m;
ost2cent = (int)(ostanek / 0.02m);
ostanek = ostanek % 0.02m;
ost1cent = (int)(ostanek / 0.01m);
ostanek = ostanek % 0.01m;

当我输入90.75

时,我会收到此输出
0x500
0x200
0x100
1x50
2x20
0x10
0x5
0x2
0x1
1x50 centov
1x20 centov
0x10 centov
1x5 centov
0x2 centov
0x1 centov

这是计算结果的另一种方法:

var denominations = new[]
{
    500m, 200m, 100m, 50m, 20m, 10m, 5m, 2m, 1m,
    0.5m, 0.2m, 0.1m, 0.05m, 0.02m, 0.01m
};

var amount = Convert.ToDecimal(Console.ReadLine());

var change = new List<decimal>();
var remaining = amount;
foreach (var denomination in denominations)
{
    while (remaining >= denomination)
    {
        remaining -= denomination;
        change.Add(denomination);
    }
}

var display =
    String.Join(
        Environment.NewLine,
        change
            .GroupBy(x => x)
            .Select(x => String.Format("{0}x{1}", x.Count(), x.Key)));

使用90.75输入,我得到了这个输出:

1x50
2x20
1x0.5
1x0.2
1x0.05