-
-
Save thomaslevesque/39090dbd0d4529f4e4b4 to your computer and use it in GitHub Desktop.
Solution pour "Les billets du Curiolande" http://enigme.blog.lemonde.fr/2016/01/27/les-billets-du-curiolande/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run in LINQPad | |
void Main() | |
{ | |
var noteValues = new[] { 501, 251, 126, 64, 33 }; | |
var notes = MakeAmount(1000, noteValues); | |
notes.GroupBy(v => v).Select(g => new { Value = g.Key, Count = g.Count() }).Dump(); | |
} | |
IEnumerable<int> MakeAmount(int remaining, IEnumerable<int> noteValues) | |
{ | |
var validNotes = noteValues.Where(v => v <= remaining).ToList(); | |
foreach (var value in validNotes) | |
{ | |
var list = new List<int> { value }; | |
if (value == remaining) | |
return list; | |
var rest = MakeAmount(remaining - value, validNotes); | |
if (rest != null) | |
{ | |
list.AddRange(rest); | |
return list; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment