日期:2014-05-17 浏览次数:21029 次
string[] GetStrings(string baseStr, int count) { char[] baseChars = baseStr.ToCharArray(); int b = baseStr.Length; int retLen = 0, i = 0; try { retLen = (int)Math.Pow(b, count); } catch { throw new ArgumentOutOfRangeException("count"); } string[] results = new string[retLen]; int[] baseArr = new int[count]; for (i = 0; i < count; i++) baseArr[i] = 0; //初始化 for (i = 0; i < retLen; i++) { results[i] = new string(baseArr.Select(x => baseChars[x]).ToArray()); //构建字符串 baseArr[count - 1] += 1; //加1 for (int j = count - 1; j > 0; j--) //进位 { if (baseArr[j] >= b) { baseArr[j - 1]++; baseArr[j] = 0; } } } return results; }
------解决方案--------------------
http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html
------解决方案--------------------
class Program { static void Main(string[] args) { Comb("abc", 3).ToList().ForEach(Console.WriteLine); } private static string Tr(string str, int n) { string s = ""; int len = str.Length; while (n >= 0) { s = str[n % len] + s; n = n / len - 1; } return s; } private static IEnumerable<string> Comb(string str, int n) { int x = str.Length; int leng = (int)Math.Pow(x, n); var sArr = new string[leng]; int t = ((int)(Math.Pow(x, n) - x) / (x - 1)); for (int i = t; i < t + leng; i++) { sArr[i - t] = Tr(str, i); } return sArr; } }