简体   繁体   English

C++ 中的帕斯卡三角程序中的 output 不正确

[英]Incorrect output in Pascal's triangle program in C++

I was creating a Pascal's triangle program in C++, but the output displayed is not as expected.我在 C++ 中创建了一个帕斯卡三角程序,但显示的 output 与预期不符。

Output Expected Output 预计

        1
      1   1
    1   2   1
  1   3   3   1
 1  4   6   4   1 
1 5   10  10  5   1 

Output got Output 得到

     1 
    1 1
   1 2 1
  1 3 3 1
 1 2 2 2 1 
1 6 6 6 6 1

Till i = 4, output displayed is correct, but after that I couldn't figure out how it goes wrong.直到 i = 4,显示的 output 是正确的,但之后我无法弄清楚它是如何出错的。 Hers is the source code to get reviewed她是要审查的源代码

int main()
{ int num, a[37680], t = 0, b = 2, l;
cout<<"Enter the number of rows: ";
cin>>num;
for (int i = 1; i <= num; i++)
{
    for (int j = 1; j <= (num - i); j++)
    {
        cout<<" ";
    }
    for (int k = 1; k <= i; k++)
    {
        l = k;
        if (k == 1 || k == i)
        {
            a[t] = 1;
            cout<<a[t]<<" ";
            t+=1;
        }
        else
        {
            a[t] = a[t - b] + a[t - b - 1]; 
            cout<<a[t]<<" ";
            t+=1;
            if ( l = (i - 1) )
            {
                b+=1;
            }
        }
    }
    cout<<endl;
}
return 0;}

Equality checking in c++ is done using == and not = , so: c++ 中的平等检查是使用==而不是=完成的,所以:

if(l=(i-1))

Should be:应该:

 if(l==(i-1))

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

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