【问题标题】:Pascal's Triangle function帕斯卡三角函数
【发布时间】:2021-12-28 18:21:28
【问题描述】:

我正在尝试编写一个函数来打印出 帕斯卡三角形 直到用户输入的步骤。该程序一直运行到第 5 步,并打印出 0 1 4 0 4 1 0 而不是预期的 0 1 4 6 1 0。它从另一个列表中获取两个值,当时都是3,然后添加它们,所以我不确定它如何更改为0。 代码:

static void PascalsTri(int input)
  {
    //Declare lists starting at 0 1 0
    int counter = 0;
    int[] start = { 0, 1, 0 };
    List<int> TriList = new List<int>(start);
    List<int> TempTriList = new List<int>();
    //Check if input is possible
    if(input < 1)
    {
      return;
    }
    //Write starting list to console
    Console.WriteLine(string.Join(" ", TriList));
    //Run the function as many times as the user input
    for(int i = 1; i < input; i++)
    {
      //Start off with the first two digits
      TempTriList.Add(0);
      TempTriList.Add(1);
      //Loop through writing the rule for as many numbers as there are
      while(counter < i)
      {
        //Takes the previous number and adds it to the correlating number
        TempTriList.Insert(counter+1, TriList[counter] + TriList[counter+1]);
        counter++;
      }
      TempTriList.Add(0);
      TriList.Clear();
      //Records the output in the permanent list, and prints it to the console
      foreach(int j in TempTriList)
      {
        TriList.Add(TempTriList[j]);
      }
      TempTriList.Clear();
      counter = 0;
      Console.WriteLine(string.Join(" ", TriList));
    }
  }

【问题讨论】:

    标签: c# algorithm


    【解决方案1】:

    如果您想逐行打印 Pascal Triangle,您可以在计算 next 行时循环 (fiddle)

    代码:

        static void PascalTri(int rowsToPrint) {
          // 1st current line
          int[] current = new int[] { 0, 1, 0 };
          
          Console.WriteLine(string.Join(" ", current));
    
          // we compute all the other lines in a simple loop
          for (int r = 1; r < rowsToPrint; ++r) {
            // next line is 1 number longer than current
            int[] next = new int[current.Length + 1];
    
            // compute next line items
            for (int c = 0; c < current.Length - 1; ++c)
              next[c + 1] = current[c] + current[c + 1];
    
            // after computation, next line becomes current one
            current = next;
    
            // finally, we print the line
            Console.WriteLine(string.Join(" ", current));
          }
        }
    

    演示:

    PascalTri(10);
    

    结果:

    0 1 0
    0 1 1 0
    0 1 2 1 0
    0 1 3 3 1 0
    0 1 4 6 4 1 0
    0 1 5 10 10 5 1 0
    0 1 6 15 20 15 6 1 0
    0 1 7 21 35 35 21 7 1 0
    0 1 8 28 56 70 56 28 8 1 0
    0 1 9 36 84 126 126 84 36 9 1 0
    

    【讨论】:

    • 啊,非常感谢,这要简单得多,也能说明程序试图完成的任务。下一次我会尝试更好的逻辑系统,而不是专注于代码。
    【解决方案2】:

    这也让我难过一阵子。然后我意识到你foreach中的一行代码是错误的。

    TriList.Add(TempTriList[j]);

    应该是:

    TriList.Add(j);

    变量“j”不是索引,而是数组值本身。一旦你做出改变,一切都会奏效。

    如果使用递归方法,此代码可能会更好地安排,但由于您拥有的工作(现在),我会留下它。使用递归进行更改可能很困难,并且远远超出了这个问题的范围。

    【讨论】:

    • 感谢您的发现!我不知道 foreach 循环的索引直接引用了列表,但现在这很有意义。我也没有想过使用递归方法,由于我对它们了解不多,所以我会研究如何使用它们并在未来改进我的代码。
    • @Gloop,是的,foreach 直接在列表/数组/其他元素上循环,而不是给你一个索引。这个概念将帮助你学习 Linq 和类似的东西。需要注意的是,在foreach 中,您不能使用加法或减法修改列表/数组/任何内容,并且向前和向后查找比简单索引要困难得多。您可以修改集合中的对象/元素,但不能修改集合本身。此外,递归可能非常棘手,可能会让你陷入糟糕的境地,所以要小心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多