Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace NestedLoopsToRecursion
- {
- class Program
- {
- static int solutions = 0;
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Stopwatch stopwatch = new Stopwatch();
- int[] arr = new int[n];
- stopwatch.Start();
- GenCombs(arr, 0,1, n);
- stopwatch.Stop();
- Console.WriteLine(solutions);
- Console.WriteLine(stopwatch.Elapsed);
- Console.ReadLine();
- }
- static void GenCombs(int[] arr, int index, int startNum, int endNum)
- {
- if (index >= arr.Length)
- {
- // A combination was found --> print it
- Console.WriteLine("(" + String.Join(", ", arr) + ")");
- solutions++;
- }
- else
- {
- for (int i = startNum; i <= endNum; i++)
- {
- if (i <= 0) continue;
- arr[index] = i;
- if (i == 1) GenCombs(arr, index + 1, i, endNum);
- else
- {
- GenCombs(arr, index + 1, endNum -i*endNum/2, endNum);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement