Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
39
Task/Combinations/C-sharp/combinations-1.cs
Normal file
39
Task/Combinations/C-sharp/combinations-1.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static IEnumerable<int[]> Combinations(int m, int n)
|
||||
{
|
||||
int[] result = new int[m];
|
||||
Stack<int> stack = new Stack<int>();
|
||||
stack.Push(0);
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
int index = stack.Count - 1;
|
||||
int value = stack.Pop();
|
||||
|
||||
while (value < n)
|
||||
{
|
||||
result[index++] = ++value;
|
||||
stack.Push(value);
|
||||
|
||||
if (index == m)
|
||||
{
|
||||
yield return result;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
foreach (int[] c in Combinations(3, 5))
|
||||
{
|
||||
Console.WriteLine(string.Join(",", c));
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Task/Combinations/C-sharp/combinations-2.cs
Normal file
36
Task/Combinations/C-sharp/combinations-2.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static IEnumerable<int[]> FindCombosRec(int[] buffer, int done, int begin, int end)
|
||||
{
|
||||
for (int i = begin; i < end; i++)
|
||||
{
|
||||
buffer[done] = i;
|
||||
|
||||
if (done == buffer.Length - 1)
|
||||
yield return buffer;
|
||||
else
|
||||
foreach (int[] child in FindCombosRec(buffer, done+1, i+1, end))
|
||||
yield return child;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<int[]> FindCombinations(int m, int n)
|
||||
{
|
||||
return FindCombosRec(new int[m], 0, 0, n);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
foreach (int[] c in FindCombinations(3, 5))
|
||||
{
|
||||
for (int i = 0; i < c.Length; i++)
|
||||
{
|
||||
Console.Write(c[i] + " ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Task/Combinations/C-sharp/combinations-3.cs
Normal file
21
Task/Combinations/C-sharp/combinations-3.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
class Combinations
|
||||
{
|
||||
static int k = 3, n = 5;
|
||||
static int [] buf = new int [k];
|
||||
|
||||
static void Main()
|
||||
{
|
||||
rec(0, 0);
|
||||
}
|
||||
|
||||
static void rec(int ind, int begin)
|
||||
{
|
||||
for (int i = begin; i < n; i++)
|
||||
{
|
||||
buf [ind] = i;
|
||||
if (ind + 1 < k) rec(ind + 1, buf [ind] + 1);
|
||||
else Console.WriteLine(string.Join(",", buf));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue