Advertisement
martixy

Euler P31 Solution

Dec 28th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Euler_P31
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.BufferHeight = 200;
  12.             // Count the number of ways to make change for targetSum using the denominations in coinSet
  13.             int[] coinSet = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000 }; //, 10, 20, 50, 100 };
  14.             int targetSum = 200;
  15.  
  16.             Console.WriteLine(CountSubsetSums(ref coinSet, targetSum));
  17.  
  18.         }
  19.  
  20.         public static ulong CountSubsetSums(ref int[] set, int targetSum)
  21.         {
  22.             Stack<int> myStack = new Stack<int>();
  23.             int stackTotal = 0;
  24.             int i = 0;
  25.             ulong count = 0;
  26.  
  27.             int[] mySet = set.Where(g => g <= targetSum).ToArray();
  28.             mySet = mySet.OrderByDescending(g => g).ToArray();
  29.             bool modeAscending = true;
  30.             while (true)
  31.             {
  32.                 if (stackTotal >= targetSum)
  33.                 {
  34.                     if (stackTotal == targetSum)
  35.                     {
  36.                         count++;
  37.                         /*
  38.                         foreach (int item in myStack)
  39.                         {
  40.                             Console.Write(item + ", ");
  41.                         }
  42.                         Console.WriteLine();
  43.                         */
  44.                     }
  45.  
  46.                     if (modeAscending || i != mySet.Length - 1)
  47.                         i = Array.IndexOf(mySet, myStack.Peek()) + 1;
  48.                     if (i == mySet.Length)
  49.                         break;
  50.                     while (myStack.Count != 0 && myStack.Peek() <= mySet[i]) // Depends on the order of the conditions
  51.                     {
  52.                         stackTotal -= myStack.Pop();
  53.                     }
  54.  
  55.                     if (!modeAscending)
  56.                         i = Array.IndexOf(mySet, myStack.Peek()) + 1;
  57.  
  58.                     stackTotal -= myStack.Pop();
  59.                     if (i == mySet.Length - 1 && modeAscending)
  60.                         modeAscending = false;
  61.                 }
  62.                 else
  63.                 {
  64.                     if (stackTotal == 0)
  65.                         modeAscending = true;
  66.                     myStack.Push(mySet[i]);
  67.                     stackTotal += mySet[i];
  68.                 }
  69.             }
  70.             return count;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement