全排列,递归。
来源:互联网 发布:手机淘宝h5页面签到 编辑:程序博客网 时间:2024/05/20 07:58
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string[] charString = { "L", "O", "V", "E" }; test(charString, 5); for (int i = 0; i < Total.Count; i++) { Console.WriteLine(Total[i]); } Console.ReadKey(); } List<string> Total = new List<string>(); /// <summary> /// 全排列 /// </summary> /// <param name="charString">排列源字符</param> /// <param name="count">排列数目</param> /// <param name="str"></param> public void test(string[] charString, int count, string str="") { foreach (string wd in charString) { string newStr = str + wd; if (newStr.Length > count-1) { Total.Add(newStr); } else { test(charString, count, newStr); } } } }}