Advertisement
gabi11

Algorithms - 02. Nested Loops

Sep 7th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Algorithms
  5. {
  6.     class Program
  7.     {
  8.         static int[] combinations;
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             combinations = new int[n];
  13.             PrintCombinations(0, n);
  14.         }
  15.  
  16.         private static void PrintCombinations(int index, int n)
  17.         {
  18.             if (index == n)
  19.             {
  20.                 PrintSolution();
  21.                 return;
  22.             }
  23.  
  24.             for (int i = 1; i <= n; i++)
  25.             {
  26.                 combinations[index] = i;
  27.                 PrintCombinations(index + 1, n);
  28.             }
  29.         }
  30.  
  31.         private static void PrintSolution()
  32.         {
  33.             Console.WriteLine(string.Join(" ", combinations)); ;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement