A,B,C,D 4个字符生成不同的排列 (AB 和BA 当成是相同的字符串ABC侧不相同)
static System.Collections.Specialized.StringCollection MakeStrings(string[] characters, int finalStringLength) { int finalLength = finalStringLength; // The length of the final strings. System.Collections.Specialized.StringCollection existingStrings; System.Collections.Specialized.StringCollection newStrings = new System.Collections.Specialized.StringCollection(); // Start with the all one-letter combinations. newStrings.AddRange(characters); for (int len = 2; len <= finalLength; len++) { // Start a new collection of strings based on the existing strings. existingStrings = newStrings; newStrings = new System.Collections.Specialized.StringCollection(); // Concatenate every string of length (len-1)... foreach (string str in existingStrings) { // ...with every character... foreach (string ch in characters) { // ...to create every possible string of length len. newStrings.Add(str + ch); } } } return newStrings; }
你是想求不同的组合吗?我用java写的,你看看是不是你需要的(递归写的,所以效率低一点):
public class Test { public static void main(String[] args) { Test t = new Test(); t.get("", 0); } private char [] chs = new char [] {'A', 'B', 'C', 'D'}; public void get(String prev, int i) { if (i >= chs.length) { System.out.println(prev); return; } get(prev, i+1); get(prev + String.valueOf(chs[i]), i+1); } }