Advertisement
stak441

Arrays - problem 20

Jan 13th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1.  static void Main(string[] args)
  2.         {
  3.             int k = int.Parse(Console.ReadLine());
  4.             int n = int.Parse(Console.ReadLine());
  5.             int[] array = new int[k];
  6.  
  7.             GetVariations(array, 0, n);            //Input given consists of: array(with defined size), starting index 0 and the max number
  8.            
  9.         }
  10.  
  11.         private static void GetVariations(int[] array, int k, int n)
  12.         {
  13.             if (k == array.Length)          //Recursion bottom - when k reaches max the result is printed
  14.             {
  15.                 PrintResult(array);
  16.             }
  17.             else
  18.             {
  19.                 for (int i = 1; i <= n; i++)
  20.                 {
  21.                     array[k] = i;
  22.                     GetVariations(array, k + 1, n);     //The recursive element increases the index k with 1 on each call
  23.                 }
  24.             }
  25.         }
  26.  
  27.         private static void PrintResult(int[] array)
  28.         {
  29.             foreach (var number in array)
  30.             {
  31.                 Console.Write(number + " ");
  32.             }
  33.             Console.WriteLine();
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement