-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeMachine.cs
More file actions
54 lines (44 loc) · 1.54 KB
/
Copy pathChangeMachine.cs
File metadata and controls
54 lines (44 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
namespace ChangeMachineProblem
{
public class ChangeMachine
{
private static readonly double[] _denominations = {20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01};
public IDictionary<double, int> MakeChange(double amount, double? wantCoin = null)
{
var results = new Dictionary<double, int>();
if (amount <= .01 || amount > 20.00)
{
throw new ArgumentException();
}
var amountLeft = amount;
if (wantCoin.HasValue)
{
results.Add(wantCoin.Value, 1);
amountLeft = Math.Round(amountLeft - wantCoin.Value, 2);
}
var startingPoint = 0;
for (var i = 0; i < _denominations.Length; i++)
{
if (_denominations[i] >= amount)
{
continue;
}
startingPoint = i;
break;
}
for (var i = startingPoint; i < _denominations.Length; i++)
{
var denomination = _denominations[i];
var coins = (int) Math.Floor(amountLeft / denomination);
amountLeft = Math.Round(amountLeft - (coins * denomination), 2);
if (results.ContainsKey(denomination))
results[denomination] += coins;
else
results.Add(denomination, coins);
}
return results;
}
}
}