Advertisement
stanevplamen

02.1.21.NumberCombinations

May 22nd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2.  
  3. class NumberCombinations
  4. {
  5.     static void Combinations(int index, int[] vector, int numbersLength, int start)
  6.     {
  7.         if (index == -1)
  8.         {
  9.             Print(vector);
  10.         }
  11.         else
  12.         {
  13.             for (int i = start; i <= numbersLength; i++)
  14.             {
  15.                 vector[index] = i;
  16.                 Combinations(index - 1, vector, (numbersLength), (i + 1));
  17.             }
  18.         }
  19.     }
  20.     static void Print(int[] vector)
  21.     {
  22.         foreach (int i in vector)
  23.         {
  24.             Console.Write("{0} ", i);
  25.         }
  26.         Console.WriteLine();
  27.     }
  28.  
  29.     static void Main()
  30.     {
  31.         Console.Write("Please enter the amount of the numbers 1...N, N = ");
  32.         int numbersAmount = int.Parse(Console.ReadLine());
  33.         Console.Write("Please enter the amount of the combinations K = ");
  34.         int combinationsAmount = int.Parse(Console.ReadLine());
  35.         int[] vector = new int[combinationsAmount];
  36.         Combinations(combinationsAmount - 1, vector, numbersAmount, 1);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement