这叫做全排列。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "ABCD";
List<string> list = new List<string>();
foreach (var i in Combo(s, 4))
{
list = list.Union(Arrange(i)).ToList();
}
list.ForEach(x => Console.WriteLine(x));
}
static IEnumerable<string> Arrange(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (source.Length == 1)
{
yield return source;
}
else
{
foreach (var x in Arrange(source.Substring(0, i) + source.Substring(i + 1)))
{
yield return source[i] + x;
}
}
}
} static IEnumerable<string> Combo(string source, int len)
{
int[] pos = new int[len];
for (int i = 0; i < len; i++) pos[i] = i;
while (pos[0] < source.Length - len)
{
string str = "";
for (int i = 0; i < len; i++) str += source[pos[i]];
for (int i = len - 1; i >= 0; i--)
{
if (pos[i] < source.Length - len + i)
{
pos[i]++;
for (int j = i + 1; j <= len - 1; j++)
{
pos[j] = pos[i] + j - i;
}
break;
}
else
{
continue;
}
}
yield return str;
}
yield return source.Substring(source.Length - len);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "ABCD";
List<string> list = new List<string>();
foreach (var i in Combo(s, 4))
{
list = list.Union(Arrange(i)).ToList();
}
list.ForEach(x => Console.WriteLine(x));
}
static IEnumerable<string> Arrange(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (source.Length == 1)
{
yield return source;
}
else
{
foreach (var x in Arrange(source.Substring(0, i) + source.Substring(i + 1)))
{
yield return source[i] + x;
}
}
}
} static IEnumerable<string> Combo(string source, int len)
{
int[] pos = new int[len];
for (int i = 0; i < len; i++) pos[i] = i;
while (pos[0] < source.Length - len)
{
string str = "";
for (int i = 0; i < len; i++) str += source[pos[i]];
for (int i = len - 1; i >= 0; i--)
{
if (pos[i] < source.Length - len + i)
{
pos[i]++;
for (int j = i + 1; j <= len - 1; j++)
{
pos[j] = pos[i] + j - i;
}
break;
}
else
{
continue;
}
}
yield return str;
}
yield return source.Substring(source.Length - len);
}
}
}
const int n = 6;
protected void Page_Load(object sender, EventArgs e)
{
int[] a = new int[n];
pai(a, 0, n);
}
private void printf(int[] a)
{
for (int i = 0; i < a.Length; i++)
Response.Write(a[i].ToString());
Response.Write("<br />");
}
private bool jiancha(int[] a, int t)
{
for (int i = 0; i < a.Length; i++)
if (a[i] == t)
return false;
return true;
}
private void pai(int[] a, int i, int j)
{
if (i >= j)
printf(a);
for (int t = 1; t <= n; t++)
{
if (jiancha(a, t))
{
a[i] = t;
pai(a, i + 1, j);
a[i] = 0;
}
}
}
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
排列组合("ABC", 0, 3).ToList().ForEach(x => { Console.WriteLine(x); });
Console.ReadKey();
} private static IEnumerable<string> 排列组合(string source, int fr, int len)
{
if (len == 1)
yield return source.Substring(fr, 1);
else if (len > 1)
{
foreach (var sub in 排列组合(source, fr + 1, len - 1))
for (var i = 0; i <= sub.Length; i++)
yield return sub.Insert(i, source.Substring(fr, 1));
}
}
}
}
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
排列组合("ABC", 0, 3).ToList().ForEach(x => { Console.WriteLine(x); });
Console.ReadKey();
} private static IEnumerable<string> 排列组合(string source, int fr, int len)
{
if (len == 1)
return new List<string> { source.Substring(fr, 1) };
else
return from sub in 排列组合(source, fr + 1, len - 1)
from i in Enumerable.Range(0, sub.Length + 1)
let first = source.Substring(fr, 1)
select sub.Insert(i, first);
}
}
}
很简单的查询,不用写一大堆麻烦的for循环之类的代码!
.ToList()
.ForEach(x => { Console.WriteLine("第{0:D2}个排列是:{1}", x.i + 1, x.s); });
这些版本支持,我在vs2005 总是报语法错误.请问是不是要在大于2.0版本之上才支持哦。谢!
只是调用部分略微需要修改下。
P(n) = n * P(n-1)
P(n-1) = (n-1) * P(n-2)
.....
P(1) = 1
结果自然就是: P(n) = n!
验证完毕看来一定要好好学习Linq把很多东西都简化了