namespace MyNameSpace
{
class GenerateWords
{
public static void Main()
{
string str = "1234";
ArrayList result = new ArrayList();
ArrayList kinds = Generate_Permutations1(str);
for (int i = 0; i < kinds.Count; i++)
{
ArrayList res = Generate_Permutations(kinds[i].ToString());
result.AddRange(res);
}
Console.WriteLine("Count = " + result.Count);
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(result[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
if (word.Length == 1)
{
result.Add(word);
return result;
}
for (int i = 0; i < word.Length; i++)
{
string shorter_word = word.Substring(0, i) + word.Substring(i + 1, word.Length - i - 1);
ArrayList shorter_Permutations = Generate_Permutations(shorter_word);
for (int j = 0; j < shorter_Permutations.Count; j++)
{
string longer_word = word[i].ToString() + shorter_Permutations[j].ToString();
result.Add(longer_word);
}
}
return result;
}
public static ArrayList Generate_Permutations1(string word)
{
ArrayList result = new ArrayList();
result.Add(word[word.Length - 1]);
if (word.Length <= 1)
return result;
for (int i = word.Length - 2; i >= 0; i--)
{
int count = result.Count;
for (int j = 0; j < count; j++)
{
result.Add(word[i].ToString() + result[j].ToString());
}
result.Add(word[i].ToString());
}
return result;
}
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;
}
}
}
}
}