Advertisement
APXOHT

Untitled

Jan 6th, 2013
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class AllCombinations
  5. {
  6.     static int N = int.Parse(Console.ReadLine());
  7.     static int K = int.Parse(Console.ReadLine());
  8.  
  9.     //generates variations
  10.     static void Combinations(int[] array, int index, int currentNumber)
  11.     {
  12.         if (index == array.Length)
  13.         {
  14.             PrintArray(array);
  15.         }
  16.         else
  17.         {
  18.             for (int i = currentNumber; i <= N; i++)
  19.             {
  20.                 array[index] = i;
  21.                 Combinations(array, index + 1, i+1);
  22.             }
  23.         }
  24.     }
  25.  
  26.     //prints array
  27.     static void PrintArray(int[] array)
  28.     {
  29.         for (int i = 0; i < array.Length; i++)
  30.         {
  31.             Console.Write(array[i] + " ");
  32.         }
  33.         Console.WriteLine();
  34.     }
  35.  
  36.     static void Main()
  37.     {
  38.         /*
  39.         //TO DO: raboti ama ne e podredeno, da se optimizira!!! (Nia veroqtno s rekursiq)
  40.         Console.Write("Enter N: ");
  41.         int N = int.Parse(Console.ReadLine());
  42.         Console.Write("Enter K: ");
  43.         int K = int.Parse(Console.ReadLine());
  44.         int maxi = (int)Math.Pow(2, N) - 1;
  45.         int checkedNumbers = 0;
  46.         List<int> combinations = new List<int>();
  47.  
  48.         for (int i = 1; i <= maxi; i++)
  49.         {
  50.             for (int j = 1; j <= N; j++)
  51.             {
  52.                 if (((i >> (j - 1)) & 1) == 1)
  53.                 {
  54.                     checkedNumbers++;
  55.                     combinations.Add(j);
  56.                 }
  57.             }
  58.             if (checkedNumbers == K)
  59.             {
  60.                 for (int j = 0; j < combinations.Count; j++)
  61.                 {
  62.                     Console.Write(combinations[j] + " ");
  63.                 }
  64.                 Console.WriteLine();
  65.             }
  66.             checkedNumbers = 0;
  67.             combinations.Clear();
  68.         }
  69.          */
  70.  
  71.         int[] array = new int[K];
  72.         Combinations(array, 0, 1);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement