Advertisement
gabi11

Algorithms - 05. Combinations without Repetition

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