杭电2032 http://acm.hdu.edu.cn/showproblem.php?pid=2032

杭电2032 杨辉三角

 

题目大意:

纯粹的杨辉三角。

注意递推公式即可。

a[i][j]=a[i-1][j]+a[i-1][j-1];

其他没什么要说的,注意最后每个杨辉三角完成后要打一个换行才能AC,否则会报PE.

 

AC代码:

#include <iostream>
using namespace std;
int n;
int a[40][40];
int main() {
    while(cin>>n) {
        a[0][0]=1;//用于初始化
        a[0][1]=0;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=i;j++) {
                a[i][j]=a[i-1][j-1]+a[i-1][j];
                if(j!=i) {
                    cout<<a[i][j]<<" ";
                } else {
                    cout<<a[i][j]<<endl;
                }
            }
        }cout<<endl; //注意换行
    }
    return 0;
}

 

其实杨辉三角的第n行是第n-1行(即减去第一行)的完全组合数。

如:

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

//第六行依次为C(5,n)(n=0,1,2,3,4,5)

 

相关文章:

  • 2021-07-15
猜你喜欢
  • 2022-12-23
  • 2021-12-25
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案