帕斯卡';C+中的s三角形+; 我试图在C++中生成一个Pascal三角形。但我不能那样做!我的代码怎么了 #include <iostream> using namespace std; int main() { int rows = 5; int currentNumber = 1; int numbers; int tempNumber; bool odd; for(int i = 0; i < rows; i++) { numbers = i+1; if(i%2 != 0) { odd = true; } else { odd = false; } for(int j = 0; j < (rows/2)-(numbers/2); j++) { cout << " "; } if(odd) { cout << " "; } currentNumber = 1; tempNumber = 0; for(int k = 0; k < numbers; k++) { cout << currentNumber << " "; if(k > numbers/2) { currentNumber-=tempNumber; tempNumber-=currentNumber; } else { currentNumber+=tempNumber; tempNumber+=currentNumber; } } cout << endl; } cout << "test"; }

帕斯卡';C+中的s三角形+; 我试图在C++中生成一个Pascal三角形。但我不能那样做!我的代码怎么了 #include <iostream> using namespace std; int main() { int rows = 5; int currentNumber = 1; int numbers; int tempNumber; bool odd; for(int i = 0; i < rows; i++) { numbers = i+1; if(i%2 != 0) { odd = true; } else { odd = false; } for(int j = 0; j < (rows/2)-(numbers/2); j++) { cout << " "; } if(odd) { cout << " "; } currentNumber = 1; tempNumber = 0; for(int k = 0; k < numbers; k++) { cout << currentNumber << " "; if(k > numbers/2) { currentNumber-=tempNumber; tempNumber-=currentNumber; } else { currentNumber+=tempNumber; tempNumber+=currentNumber; } } cout << endl; } cout << "test"; },c++,pascals-triangle,C++,Pascals Triangle,我要做的第一件事就是不用担心打印时数字的实际排列。这在所有编程中都是一个类似的概念,您希望将事物的显示方式、“视图”与数据中事物的显示方式、“模型”分离开来。既然你有一个构造代码的概念基础,我也会使用类结构和对象,因为你说C++是你正在尝试的,主要集中在面向对象编程(OOP)范例上。有了这些想法,我们就可以开始想出一种方法来创建三角形 我们希望实现以下结构的程序化生产: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 为了实现这一效果,我们确实希望创造

我要做的第一件事就是不用担心打印时数字的实际排列。这在所有编程中都是一个类似的概念,您希望将事物的显示方式、“视图”与数据中事物的显示方式、“模型”分离开来。既然你有一个构造代码的概念基础,我也会使用类结构和对象,因为你说C++是你正在尝试的,主要集中在面向对象编程(OOP)范例上。有了这些想法,我们就可以开始想出一种方法来创建三角形

我们希望实现以下结构的程序化生产:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
为了实现这一效果,我们确实希望创造:

1
1,1
1,2,1
1,3,3,1
1,4,6,4,1
如果您注意到上面的图表,我们可以从axiom(第一次迭代/在我们开始之前假设存在的东西)开始推断,在这种情况下,它只是一行,只包含数字
1
,我们可以看到每个产品(任何给定的迭代,都有一些规则来产生它,首先从公理开始,
1
)通过将上面一行中具有相同索引的两个值和下一个索引相加得到。为了算法起见,我们假设如果产品找不到一个数字,我们会说该数字存在但未显示,并且我们会假设它为0。(如果一直在左侧,则没有第一个值,如果一直在右侧,则没有第二个值)

这意味着如果你看我的第二张图,我们有第一行,它只是
1
,但我们要假装它真的是
0,1,0
。这给了我们一个金字塔,更像:

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
最重要的部分是公理
1
,我们假设它是
0,1,0
也就是说,创建一行的方法,给定它前面的行。我们希望创建一个函数,该函数将从上一行中获取一个数字列表,并创建下一行,假设它的两边都填充了我们的
0
s。该函数可能如下所示:

std::vector<int> produceNextRow(std::vector<int> previousRow) {
  std::vector<int> nextRow;

  int left, right = 0;
  /* For each of the numbers in the previous row, plus one more
  because each next row has one more value than the last */
  for( int i = 0; i < previousRow.size()+1 ; i++ ) {
    
    // If we're all the way on the left we need 'left' to be our imaginary 0
    if( i == 0 ) {
      left = 0;
    } else {
      /* Otherwise we want it to be the value in the same position of the
      previous row which is slightly to the left, hence -1 */
      left = previousRow[i-1];
    }

    // If we're all the way on the right, we need 'right' to be our imaginary 0
    if( i == previousRow.size() ) {
      right = 0;
    } else {
     /* Otherwise we want it to be the value of the previous row, at the same
      position */
      right = previousRow[i];
    }

    /* Finally we add left and right, to get our new number, then we put it into
    the next row */
    nextRow.push_back(left+right);
  }

  // Let's return our new row we created
  return nextRow;
}
std::vector produceNextRow(std::vector previousRow){
std::vector nextRow;
int左,右=0;
/*对于上一行中的每个数字,再加上一个
因为下一行比上一行多出一个值*/
对于(int i=0;i
实际上,这就是我们所需要的。现在,您只需要使用一个技巧,在它们之间放置空格并打印出来。我在这里不这样做,但我将向您展示如何使用此函数生成几行三角形:

int main(int argc, char *argv[]) {

  // Vector of vectors of ints! This is our entire triangle
  std::vector<std::vector<int>> triangle;
  std::vector<int> axiom {1};

  // Lets put our axiom into the triangle first
  triangle.push_back(axiom);

  // Alright, for the sake of example lets do some number of productions like 6
  int productions = 6;
  for( int i=0; i < productions ; i++ ) {
    triangle.push_back(produceNextRow(triangle[i]));
  }

  /* Now lets print out our triangle, you'd replace this with something fancy
  that maybe computes how many spaces are required, and makes the triangle look
  better. I'll leave that as an exercise for you */

  // For each row in the triangle
  for( int i=0; i < triangle.size() ; i++ ) {
    // For each number in the row
    for( int j=0; j < triangle[i].size() ; j++ ) {
      // print that number followed by a comma and a space
      std::cout << triangle[i][j] << ", ";
    }
    // print a new line after each row
    std::cout << std::endl;
  }

  return 0;
}
intmain(intargc,char*argv[]){
//整数向量的向量!这是我们的整个三角形
向量三角形;
向量公理{1};
//让我们先把我们的公理放到三角形中
三角形。推回(公理);
//好的,为了举例,让我们做一些制作,比如6
int=6;
对于(int i=0;istd::cout我要做的第一件事是不担心打印时数字的实际排列。这在所有编程中都是一个类似的概念,您希望将事物如何显示、“视图”与数据中的事物如何“模型”分开既然你有一个构造代码的概念基础,我也会使用类结构和对象,因为你说C++是你正在尝试的,主要集中在面向对象编程(OOP)上。范例。有了这些想法,我们就可以开始想出一种方法来创建三角形

我们希望实现以下结构的程序化生产:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
为了实现这一效果,我们确实希望创造:

1
1,1
1,2,1
1,3,3,1
1,4,6,4,1
如果您注意到上面的图表,我们可以从axiom(第一次迭代/在我们开始之前假设存在的东西)开始推断,在这种情况下,它只是一行,只包含数字
1
,我们可以看到每个产品(任何给定的迭代,都有一些规则来产生它,首先从公理开始,
1
)通过将上面一行中具有相同索引的两个值和下一个索引相加得到。为了算法起见,我们将假设如果产品找不到一个数字,我们将说该数字存在但未显示,我们将假设它为0。(如果它一直在左边,则有n个。)