【问题标题】:It is possible to generate random numbers sequences from digits in c#?可以从c#中的数字生成随机数序列吗?
【发布时间】: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。你可以读进去——也许这会有所帮助。
  • 好吧,我找到了代码stackoverflow.com/questions/48206913/…
  • 有人能帮我把它从JAVA翻译成c#吗?

标签: c# numbers sequence digits


【解决方案1】:

如你所愿,我将 this answer 翻译成 C#

类多米诺骨牌: `

public class Domino {
    public Domino(int a, int b)
    {
        A = a;
        B = b;
    }
    public int A { get; set; }
    public int B { get; set; }


    public Domino Flipped()
    {
        return new Domino(B, A);
    }

    public override string ToString()
    {
        return $"[{A}/{B}]";
    }

}

算法:

    public static void GetList(List<Domino> chain, List<Domino> list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            Domino domino = list[i];

            if (CanAppend(domino, chain))
            {
                chain.Add(domino);
                PrintList(chain);
                list.Remove(domino);
                //You need to create these two lists via the new keyword because 
                //we do not want to keep up the reference to the "old" list. 
                //Otherwise changes in the recoursion would also change the top-level list.
                GetList(new List<Domino>(chain), new List<Domino>(list));
                list.Insert(i, domino);
                chain.Remove(domino);
            }

            var flippedDomino = domino.Flipped();
            if (CanAppend(flippedDomino, chain))
            {
                chain.Add(flippedDomino);
                PrintList(chain);
                list.Remove(domino);
                GetList(new List<Domino>(chain), new List<Domino>(list));
                list.Insert(i, domino);
                chain.Remove(flippedDomino);
            }
        }
    }

两个辅助方法:

    public static bool CanAppend(Domino domino, List<Domino> items)
    {
        return items.Count == 0 || items.Last().B == domino.A;
    }
    private static void PrintList(List<Domino> items)
    {
        Console.WriteLine();
        foreach (var item in items)
        {
            Console.Write(item.ToString());
        }
    }

这就是你使用它的方式:

List<Domino> list = new List<Domino>();    
// [3/4] [5/6] [1/4] [1/6]   
list.Add(new Domino(3, 4));    
list.Add(new Domino(5, 6));    
list.Add(new Domino(5, 6));    
list.Add(new Domino(1, 4));    
list.Add(new Domino(1, 6));    
List<Domino> chain = new List<Domino>();    
GetList(chain, list);

【讨论】:

  • 如果您觉得这回答了您的问题,请考虑将其标记为正确的问题。 (而且我真的很喜欢声誉)
  • 好的。最后一个问题:您能解释一下为什么要从列表中删除多米诺骨牌,然后再添加一次吗?我知道你这样做是因为递归,但我不确定它是如何工作的......我只知道方法调用自己......
  • 你必须用翻转版本重新尝试每一个多米诺骨牌。启动“新递归分支”时,无法将多米诺骨牌添加到链中并且必须在列表中 - 否则“翻转的多米诺骨牌”将已添加到列表中并且无法再添加。它(可以这么说)已经被淘汰/使用了
  • 我不确定,这些行是如何工作的:list.Insert(i, domino);链。删除(多米诺骨牌);为什么你将多米诺骨牌添加到列表中,然后在特定索引处删除?
  • 基本上我想在启动新的递归分支后重新创建以前的设置。您还可以将两个列表存储在一个临时列表中,使用移除的多米诺骨牌启动递归方法,然后使用移除的翻转多米诺骨牌启动递归方法。那将有相同的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多