Advertisement
Guest User

Untitled

a guest
Sep 19th, 2015
471
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace NestedLoopsToRecursion
  9. {
  10. class Program
  11. {
  12. static int solutions = 0;
  13. static void Main()
  14. {
  15. int n = int.Parse(Console.ReadLine());
  16. Stopwatch stopwatch = new Stopwatch();
  17. int[] arr = new int[n];
  18. stopwatch.Start();
  19. GenCombs(arr, 0,1, n);
  20. stopwatch.Stop();
  21. Console.WriteLine(solutions);
  22. Console.WriteLine(stopwatch.Elapsed);
  23. Console.ReadLine();
  24. }
  25.  
  26. static void GenCombs(int[] arr, int index, int startNum, int endNum)
  27. {
  28. if (index >= arr.Length)
  29. {
  30. // A combination was found --> print it
  31. Console.WriteLine("(" + String.Join(", ", arr) + ")");
  32. solutions++;
  33. }
  34. else
  35. {
  36. for (int i = startNum; i <= endNum; i++)
  37. {
  38. if (i <= 0) continue;
  39. arr[index] = i;
  40. if (i == 1) GenCombs(arr, index + 1, i, endNum);
  41. else
  42. {
  43.  
  44. GenCombs(arr, index + 1, endNum -i*endNum/2, endNum);
  45. }
  46. }
  47.  
  48. }
  49. }
  50. }
  51. }
Advertisement
RAW Paste Data Copied
Advertisement