C# 一个单词并打印不同的三个字母组合
我必须输入一个5个字母的单词,然后它应该打印出不同的3个字母的组合,但它不工作,我不知道为什么 例如,如果输入“hello”,则应返回所有不同的组合,如“leh”、“lol”、“hle”等C# 一个单词并打印不同的三个字母组合,c#,random,printing,combinations,C#,Random,Printing,Combinations,我必须输入一个5个字母的单词,然后它应该打印出不同的3个字母的组合,但它不工作,我不知道为什么 例如,如果输入“hello”,则应返回所有不同的组合,如“leh”、“lol”、“hle”等 static void Main(string[] args) { Console.Write("Enter a five-letter word: "); String x = Console.ReadLine(); for (int i = 0; i < x.Length;
static void Main(string[] args)
{
Console.Write("Enter a five-letter word: ");
String x = Console.ReadLine();
for (int i = 0; i < x.Length; i++)
{
char letter = x[i];
Random ran = new Random();
for (int z = 1; z < 4; z++)
{
char y = x[ran.Next(0, x.Length)];
Console.Write(y);
}
}
Console.WriteLine();
}
static void Main(字符串[]args)
{
控制台。写(“输入一个五个字母的单词:”);
字符串x=Console.ReadLine();
对于(int i=0;i
要获取给定单词的所有三个字母单词,需要为三个结果字符建立三个索引:
int firstChar = 0;
int secondChar = 1;
int thirdChar = 2;
然后为这三个索引创建三个嵌套循环
第一个循环从0运行到最后一个2
第二个循环从firstChar+1运行到last-1
第三个循环从secondChar+1运行到最后一个循环
在内部循环中构建结果词:
string resultCombination = word[firstChar] + word[secondChar] + word[thirdChar];
然后使用
添加一些错误检查:
单词必须有三个或更多字符
决定如果单词两次包含同一个字符该怎么办。您可能需要一个具有唯一字符的word版本。我们可以将问题陈述分为两部分
string word = "hello";
int [,] arrIndexes = new int[9,3] {{0,1,2}, {0,1,3}, {0,1,4}, {0,2,3}, {0,2,4}, {0,3,4}, {1,2,3}, {1,3,4}, {2,3,4}};
for(int i=0; i < 9; i++)
{
string sub = "";
for(int j=0; j<3; j++)
sub += word[arrIndexes[i,j]];
Console.Write("{0}{1}{2}",sub[0], sub[1], sub[2]);
Console.Write("\t{0}{1}{2}",sub[2], sub[1], sub[0]);
Console.Write("\t{0}{1}{2}",sub[1], sub[0],sub[2]);
Console.Write("\t{0}{1}{2}",sub[0], sub[2], sub[1]);
Console.Write("\t{0}{1}{2}",sub[1], sub[2], sub[0]);
Console.WriteLine("\t{0}{1}{2}",sub[2], sub[0], sub[1]);
}
编辑
hel leh ehl hle elh lhe
hel leh ehl hle elh lhe
heo oeh eho hoe eoh ohe
hll llh lhl hll llh lhl
hlo olh lho hol loh ohl
hlo olh lho hol loh ohl
ell lle lel ell lle lel
elo ole leo eol loe oel
elo ole leo eol loe oel
llo oll llo lol lol oll
hel hle ehl elh lhe leh
hel hle ehl elh lhe leh
heo hoe eho eoh ohe oeh
hll hll lhl llh lhl llh
hlo hol lho loh ohl olh
hlo hol lho loh ohl olh
ell ell lel lle lel lle
elo eol leo loe oel ole
elo eol leo loe oel ole
llo lol llo lol oll oll
您可以进一步推广它,以获得三个字母抽取单词的组合
string word = "hello";
for(int i=0; i<word.Length-2; i++)
for(int j=i+1; j< word.Length-1; j++)
for(int k=j+1; k < word.Length; k++)
{
string sub = string.Format("{0}{1}{2}",word[i], word[j], word[k]);
for(int l=0; l<3;l++)
for(int m=0; m<3;m++)
for(int n=0; n<3;n++)
if(l != m && m != n && l!=n)
Console.Write("\t{0}{1}{2}",sub[l], sub[m], sub[n]);
Console.WriteLine("");
}
你是说所有可能的三个字母组合吗?或者只是其中的几个?如果你需要所有的组合,你不应该使用random。你无论如何都不应该使用random,因为你可以重复同一个字母。如果你的输入总是5个字母的单词,为什么要使用random。您可以使用索引的排列。e、 g.230、243021。如果您想做一些改进,您可以创建一个函数,返回索引5P3的排列,然后进行比较以返回三个字母的唯一组合。如果有一些自动化的方法可以确保不会遗漏任何内容(例如,
arrdexes
的一个元素和两个控制台。写入行)并且可以扩展到不同长度的字符串。嗯,这看起来不像是一个编程算法,只是一个详细说明的固定解决方案-对程序员来说不是很有用。@Koch博士,我在重新分解代码,正如Rawling已经指出的那样。现在您编写了我的建议,看起来好多了;)现在,您只需要计算排列,这样就不会遗漏其中的2/3……例如,这不会产生hle
,因为它只以字符在输入中出现的相同顺序输出字符。但是关于for
循环的部分很好;总比手工计算好。@Rawling谢谢,你说得对。我修改了我的答案
hel hle ehl elh lhe leh
hel hle ehl elh lhe leh
heo hoe eho eoh ohe oeh
hll hll lhl llh lhl llh
hlo hol lho loh ohl olh
hlo hol lho loh ohl olh
ell ell lel lle lel lle
elo eol leo loe oel ole
elo eol leo loe oel ole
llo lol llo lol oll oll