组合1
2
3
4
5
6
7
8
9
10
11
12
13public static void Comb(int[] array, int offset, int n, int[] result)
{
if (n == 0)
{
Print(result);
return;
}
for (int i = offset; i < array.Length; i++)
{
result[result.Length - n] = array[i];
Comb(array, i + 1, n - 1, result);
}
}
排列1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public static void Perm(int[] array, int offset,int n)
{
if (offset == n)
{
Print(array, n);
return;
}
for (int i = offset; i < array.Length; i++)
{
if (i != offset)
{
Swap(ref array[i], ref array[offset]);
}
Perm(array, offset + 1, n);
if (i != offset)
{
Swap(ref array[i], ref array[offset]);
}
}
}
排列组合算法
|