Advertisement
gabi11

Algorithms - 03. Combinations with Repetition

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