简体   繁体   English

C++:帕斯卡三角

[英]C++: Pascal's Triangle

I am struggling on how to interpret and/or relate between comb(i, j) to long comb(int n, int k) .我正在努力解释和/或在comb(i, j)long comb(int n, int k)之间建立联系。 May you please explain to me how the for loop of the long comb(int n, int k) works?请您向我解释一下long comb(int n, int k)的 for 循环是如何工作的? Thank you very much.非常感谢。

#include<iostream>
#include<iomanip>
using namespace std;

long comb(int, int);

int main()
{
 int m;
 cout << "Type a number and then press ENTER: ";
 cin >> m;

 for(int i = 0; i < m; i++)
 {
  for(int j = 1; j < (m - i); j++)
  {
   cout << setw(2) << "";
  }
  for(int j = 0; j <= i; j++)
  {
   cout << setw(4) << comb(i, j);
  }
 cout << endl;
 }
}

long comb(int n, int k)
{
 if (n < 0 || k < 0 || n < k)
 {
  return 0;
 }
 long c= 1;
 for(int i = 1; i <= k; i++, n--)
 {
  c = c*(n/i);
 }
 return c;
}

Edit: Thank Yunnosch for the correction.编辑:感谢 Yunnosch 的更正。

The for loop in the long comb(int n, int k) is calculating the Binomial Coefficients using the Multiplicative Formula . long comb(int n, int k)中的for循环使用乘法公式计算二项式系数。

You could also write that for loop like below (which looks more like the multiplicative formula):您还可以编写如下的 for 循环(看起来更像是乘法公式):

for(int i = 1; i <= k; i++) {
    c = c * ((n - i + 1) / i); 
}

The

long comb(int, int);

before int main() is the declaration of the long comb(int n, int k) function.int main()之前是long comb(int n, int k) function 的声明 You have to do it, so that the code compiles.您必须这样做,以便代码编译。 Another way would be to define the long comb(int n, int k) function completely before main() instead of after it.另一种方法是在main()之前而不是之后定义long comb(int n, int k) function 。 You should understand the difference between declaration and definition in C++.您应该了解 C++ 中声明和定义的区别。

By the way, your code seems to have a bug, and does not print the correct Pascal triangle.顺便说一句,您的代码似乎有一个错误,并且没有打印正确的帕斯卡三角形。

These are the meanings of the different occurrences of comb() in your code.这些是代码中不同出现的comb()的含义。

long comb(int, int);

This means "Dear compiler. Later on I will provide code for a function of this name with these parameter types. Please accept that I call it before you have seen the code."这意味着“亲爱的编译器。稍后我将为具有这些参数类型的此名称的 function 提供代码。请接受我在您看到代码之前调用它。”
It is called a "prototype".它被称为“原型”。

cout << setw(4) << comb(i, j);

Means "Dear compiler, remember the function I told you about but have not shown you the code for? Call it here please, as part of making output; the code is below somewhere."意思是“亲爱的编译器,记得我告诉过你的 function,但没有向你展示代码吗?请在此处调用它,作为制作 output 的一部分;代码在下面某处。”

long comb(int n, int k)
{
/* ... */
}

Means "Dear compiler, with the prototype above I promised to provide code for a function of this name and parameters. I even asked you to call it from other code. Here I finally provide the code for it. Please use this code for the execution of the call from main()."意思是“亲爱的编译器,上面的原型我答应提供一个function这个名字和参数的代码。我什至让你从其他代码中调用它。这里我终于提供了它的代码。请使用此代码执行来自 main() 的调用。” (Some of this last part actually addresses more the linker than the compiler, but I think that level of detail is out of scope here.) (最后一部分中的一些实际上比编译器更多地解决了 linker,但我认为这里的详细程度超出了 scope。)

I have the impression that you don't understand how to learn how to program:我的印象是您不了解如何学习如何编程:
An experienced programmer looks at a piece of code and understands what it means, but for a starting programmer most of the times this does not work: you need to try the piece of code, debug it, try to understand it and mostly, try it out .一个有经验的程序员会看一段代码并理解它的含义,但对于初学者来说,这在大多数情况下是行不通的:你需要尝试这段代码,调试它,尝试理解它,大多数时候,尝试它出来

Let's have a look at comb(n,k) : apparently n must be larger than k, so let's have a look at what happens for the values n=8 and k=3:让我们看看comb(n,k) :显然 n 必须大于 k,所以让我们看看值 n=8 和 k=3 会发生什么:

for(int i = 1; i <= k; i++, n--)
 {
  c = c*(n/i);
 }

What happens with i and c? i 和 c 会发生什么?

   i      k      n      c
   1      3      8      1*8/1
   2      3      7      1*8/1*7/2
   3      3      6      1*8/1*7/2*6/3

So, at the end: c becomes 8*7*6/(1*2*3) .因此,最后: c 变为8*7*6/(1*2*3)

I admit: two variables get changed during the loop ( i++ and n-- ), which is confusing indeed, but by trying out you'll get the hang of it.我承认:两个变量在循环期间发生了变化( i++n-- ),这确实令人困惑,但通过尝试你会掌握它的窍门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM