Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class SumWithUnlimitedCoins
  5. {
  6. static void Main()
  7. {
  8. Console.Write("S = ");
  9. var totalSum = int.Parse(Console.ReadLine());
  10.  
  11. Console.Write("Coins = ");
  12. var coins =
  13. Console.ReadLine()
  14. .Trim('{', '}')
  15. .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  16. .Select(int.Parse)
  17. .ToArray();
  18.  
  19. var combinationsCount = new int[totalSum + 1];
  20.  
  21. // Calculate the number of possible combinations for the first coin (coins[0])
  22. for (int sum = 0; sum <= totalSum; sum++)
  23. {
  24. if (sum % coins[0] == 0)
  25. {
  26. combinationsCount[sum] = 1;
  27. }
  28. }
  29.  
  30. // Calculate the number of possible combinations for every other coin
  31. for (int coin = 1; coin < coins.Length; coin++)
  32. {
  33. for (int sum = 1; sum <= totalSum; sum++)
  34. {
  35. if (coins[coin] <= sum)
  36. {
  37. combinationsCount[sum] += combinationsCount[sum - coins[coin]];
  38. }
  39. }
  40. }
  41.  
  42. Console.WriteLine(combinationsCount[totalSum]);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement