|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program // Calculate P (M, N) M = 3; N = 3
{
static int Count = 0; // stores the number of permutations and combinations.
static void Main (string [] args)
{
int M = 3;
int N = 3;
int [] a = new int [M];
for (int i = 1; i <= M; i ++) // The array is assigned the initial value "1,2,3"
{
a [i-1] = i;
}
if (M <N)
{
}
else
{
getValue (a, 0, N);
Console.WriteLine (Count); // Number of output combinations
Console.Read ();
}
}
static void getValue (int [] a, int k, int n)
{
int temp;
if (k> = n) // output:
{
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); // recursive call
temp = a [k];
a [k] = a [i];
a [i] = temp;
}
}
}
}
} |
|