从问题?,排列?,组合生成不同路径的矩阵?

时间:2017-09-28 09:01:57

标签: c# algorithm combinations permutation

我需要一些正确方向的帮助才能开始我的项目。 让我通过举一个例子来解释这种情况:

例如,我有3个问题,可以通过真或假来回答。 我想概述所有可能的答案组合 例如:

Question 1          Question 2          Question 3  
True                True                True  
True                False               True  
False               True                False  

等等...... 有没有人知道我如何编写一个工具,最好用C#生成一个像上面这样的矩阵,并带来所有可能的结果?

非常感谢你们的反馈或想法! 问候蒂姆

2 个答案:

答案 0 :(得分:0)

完全从头开始重写。想象一下,你有N个问题(演示中有3个):

  string[][] questionsAndAnswers = new string[][] { 
    // Two times Two is Four?
    new string[] { "true", "false"},
    // What is the capital of Russia?
    new string[] { "Berlin", "Moscow", "London"},
    // Sqrt(3 * 3 + 4 * 4) = ? 
    new string[] { "1", "2", "3", "5"},
  };

首先可以回答为true/false,第二个回答 - 三个城市之一等等。然后,您可以通过简单的例程列举所有可能的答案:

private static IEnumerable<string> Generator(string[][] answers) {
  int[] indexes = new int[answers.Length];

  while (true) {
    yield return string.Join("\t", answers
      .Select((answer, idx) => answer[indexes[idx]]));

    for (int i = 0; i < indexes.Length; ++i) {
      if (indexes[i] < answers[i].Length - 1) {
        indexes[i] += 1;

        break;
      }
      else {
        if (i == indexes.Length - 1)
          yield break;

        indexes[i] = 0;
      }
    }
  }
}

例如:

  string caption = string.Join("\t", Enumerable
    .Range(1, questionsAndAnswers.Length)
    .Select(i => $"Question {i}"));

  // Or loop - foreach - instead of Join
  string body = string.Join(Environment.NewLine, Generator(questionsAndAnswers));

  string report = string.Join(Environment.NewLine, caption, body);

  Console.WriteLine(report);

结果:

Question 1  Question 2  Question 3
true    Berlin  1
false   Berlin  1
true    Moscow  1
false   Moscow  1
true    London  1
false   London  1
true    Berlin  2
false   Berlin  2
true    Moscow  2
false   Moscow  2
true    London  2
false   London  2
true    Berlin  3
false   Berlin  3
true    Moscow  3
false   Moscow  3
true    London  3
false   London  3
true    Berlin  5
false   Berlin  5
true    Moscow  5 // <- The right answer
false   Moscow  5
true    London  5
false   London  5

答案 1 :(得分:0)

如果你真的喜欢丑陋的lambdas,可以在一个linq声明中做到:

var q1 = new string[] { "true", "false" };
var q2 = new string[] { "Berlin", "Moscow", "London" };
var q3 = new string[] { "1", "2", "3", "5" };

var combos = q1.Join(q2, _q1 => true, _q2 => true, (q11, q22) => new { q11, q22 }).Join(q3, _q3 => true, _q2 => true, (q11, q33) => new {q11.q11, q11.q22,q33});
combos.Dump();

它有2个linq(交叉)连接,将列表组合成所有可能组合的最终列表。

输出:

Question1 Question2 Question3    
true Berlin 1 
true Berlin 2 
true Berlin 3 
true Berlin 5 
true Moscow 1 
true Moscow 2 
true Moscow 3 
true Moscow 5 
true London 1 
true London 2 
true London 3 
true London 5 
false Berlin 1 
false Berlin 2 
false Berlin 3 
false Berlin 5 
false Moscow 1 
false Moscow 2 
false Moscow 3 
false Moscow 5 
false London 1 
false London 2 
false London 3 
false London 5 
相关问题