我想在C#.Net中生成具有给定集和长度的所有可能字符串。
例如,设置{+,-,0,1,2,3,4,5,6,7,8,9}
(不总是数字)和长度为4:
+001 ,
001+ ,
0+01 ,
12+1 ,
02-9 ,
1502 ,
...
答案 0 :(得分:5)
char[] chars = "+-0123456789".ToCharArray();
var strings =
from a in chars
from b in chars
from c in chars
from d in chars
select new string(new[] { a, b, c, d });
答案 1 :(得分:0)
效果不佳但效果正常
List<char> input;
public static void Main()
{
input=new List<char>(){'+','-','0','1','2','3','4','5','6','7','8','9'};
List<char> permutation = new List<char>();
permutation.Add(input[0]);
permutation.Add(input[0]);
permutation.Add(input[0]);
GetPermutation(ref permutation, input.Count);
}
private static void GetPermutation(ref List<char> permutation,int length)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
for (int k = 0; k < length; k++)
{
string output = "";
permutation[0] = input[k];
permutation[1] = input[j];
permutation[2] = input[i];
output += permutation[0];
output += permutation[1];
output += permutation[2];
Console.WriteLine(output + "\n");
count++;
}
}
}
}