【问题标题】:Get n unique objects out out M collection从 M 个集合中取出 n 个唯一对象
【发布时间】:2025-12-04 12:50:01
【问题描述】:

我想知道如何在 C# 中完成这项任务。例如;

我收到了 10 个问题,其中 3 个要显示给用户,让他们 键入答案。我怎样才能让程序产生 3 个问题 假设我们有 10 个问题,是不重复的(唯一的) 开头是唯一的。

我在asp.net应用程序中使用逻辑,下次刷新页面时允许显示相同的问题集,所以对我来说没问题。

【问题讨论】:

    标签: c# asp.net logic combinations


    【解决方案1】:

    为您的问题实例使用一个列表,并随机选择一个(按索引)。然后将其从列表中删除并重复。像这样;

        static void Main(string[] args)
        {
            List<string> questions = new List<string>();
            for (int i = 0; i < 10; i++)
                questions.Add("Question " + i);
    
            Random r = new Random();
            for (int i = 0; i < 3; i++)
            {
                int nextQuestion = r.Next(0, questions.Count);
                Console.WriteLine(questions[nextQuestion]);
                questions.RemoveAt(nextQuestion);
            }
        }
    

    【讨论】:

    • 是的,你是对的。我认为出于某种原因,使用相同的 Random 对象每次使用时都会产生一个唯一的数字,但事实并非如此。您必须在某种收藏中自行跟踪它们。
    【解决方案2】:

    其中一种方法是随机打乱元素,然后选择其中的前三个。 关于如何在 C# 中洗牌 - Randomize a List<T>.
    这种方法比从列表中删除大集合中的问题要好,因为在最坏的情况下(当随机化被确定或刚刚发生很糟糕时)由于删除的 O(n) 复杂性,它可以增长到 O(n^2)。

    【讨论】:

      【解决方案3】:
      class Questions
      {
          const int NUMBER_OF_QUESTIONS = 10;
          readonly List<string> questionsList;
          private bool[] avoidQuestions; // this is the "do-not-ask-question" list
          public Questions()
          {
              avoidQuestions = new bool[NUMBER_OF_QUESTIONS];
      
              questionsList = new List<string>
                                  {
                                      "question1",
                                      "question2",
                                      "question3",
                                      "question4",
                                      "question5",
                                      "question6",
                                      "question7",
                                      "question8",
                                      "question9"
                                  };            
          }
      
          public string GetQuestion()
          {
              Random rnd = new Random();
              int randomVal;
      
              // get a new question if this question is on the "do not ask question" list
              do
              {
                  randomVal =  rnd.Next(0, NUMBER_OF_QUESTIONS -1);
              } while (avoidQuestions[randomVal]);
      
              // do not allow this question to be selected again
              avoidQuestions[randomVal] = true;
      
              // do not allow question before this one to be selected
              if (randomVal != 0)
              {
                  avoidQuestions[randomVal - 1] = true;
              }
      
              // do not allow question after this one to be selected
              if (randomVal != NUMBER_OF_QUESTIONS - 1)
              {
                  avoidQuestions[randomVal + 1] = true;
              }
      
              return questionsList[randomVal];
          }
      }
      

      只需创建 Questions 对象并调用 questions.GetQuestions() 三次

      【讨论】: