【发布时间】:2019-02-17 17:45:02
【问题描述】:
stackoverflow 社区,
我想制作 c# 应用程序,它可以生成数字序列(如果前一个数字的最后一个数字等于第二个数字的第一个数字)。例如,我有一个数据数组,其中包含:20、15、25、05、53、31,我需要创建所有可能的序列。
So, in my case it should be:
20 05 53;
02 25 53 31 15;
15 53 31;
25 53 31 15;
and etc...
给定数字的数字可以交换。在一个序列中,相同的数字只能使用一次(例如 20 和 02、15 和 51,它们在一个序列中只能使用一次) 好吧,我尝试了一些代码组合,但都没有成功......
for (int i = 0; i < data.Length; i++)
{
string lastDigit = data[i].Substring(1, 1); // setting last digit of the first number
string generatedSequence = "";
for (int c = 0; c < data.Length; c++)
{
if (lastDigit == data[c].Substring(0, 1)) //if last digit of previous number equals to first digit of next number
{
lastDigit = data[c].Substring(1, 1); // second digit of the number
generatedSequence = generatedSequence + " " + data[c];
}
}
}
【问题讨论】:
-
您是否需要所有可能的组合或目标到底是什么?
-
是的,我需要所有可能的评论
-
呃。这似乎是一件复杂的事情。该主题称为permutation。你可以读进去——也许这会有所帮助。
-
有人能帮我把它从JAVA翻译成c#吗?
标签: c# numbers sequence digits