|
发表于 2020-2-1 19:00:02
|
显示全部楼层
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program //计算P(M,N) M=3;N=3
{
static int Count=0; //存放排列组合数.
static void Main(string[] args)
{
int M = 3;
int N = 3;
int[] a = new int[M];
for (int i = 1; i <= M; i++) //数组赋初值"1,2,3"
{
a[i-1] = i;
}
if (M < N)
{
}
else
{
getValue(a, 0, N);
Console.WriteLine(Count);//输出组合数
Console.Read();
}
}
static void getValue(int[] a, int k, int n)
{
int temp;
if (k >= n) //输出结果:
{
string s = "";
for (int i = 0; i < n; i++)
{
s = s + a[i].ToString() + " ";
}
Count++;
Console.WriteLine(s);
}
else
{
for (int i = k; i < a.Length; i++)
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
getValue(a, k + 1, n); //递归调用
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
}
} |
|