using System;
using System.Collections.Generic;
public class Combination
{
public static void Main(string[] args)
{
new Combination();
}
private Combination()
{
string[] data = new string[] { "A", "B", "C", "D", "E", "F" };
int k = 3;
List<string[]> combList = GetCombination(data, k);
WriteComb(combList);
}
private List<string[]> GetCombination(string[] data, int k)
{
int dataIndex = 0;
string[] combs = new string[k];
int combsIndex = 0;
List<string[]> combList = new List<string[]>();
Comb(data, dataIndex, combs, combsIndex, combList);
return combList;
}
private void Comb(string[] data, int dataIndex, string[] combs, int combsIndex, List<string[]> combList)
{
if (combsIndex == combs.Length) {
string[] copy = new string[combs.Length];
Array.Copy(combs, copy, combs.Length);
combList.Add(copy);
return;
}
int endDataIndex = data.Length - combs.Length + combsIndex;
for (int i = dataIndex; i <= endDataIndex; i++) {
combs[combsIndex] = data[i];
Comb(data, i + 1, combs, combsIndex + 1, combList);
}
}
private void WriteComb(List<string[]> combList)
{
int n = 1;
foreach (string[] combs in combList) {
Console.WriteLine(string.Format("{0}: {1}", n, string.Join(",", combs)));
n++;
}
}
}
↧
組み合わせ アルゴリズム nCk C#
↧