题解 | 杨辉三角

杨辉三角

https://www.nowcoder.com/practice/8c6984f3dc664ef0a305c24e1473729e

#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    long long a[n][n];  a[0][0]=1;
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<=n-1;j++){
             a[i][j]=0;
        }
    }
    cout<<"1"<<endl;
    for(int i=1;i<=n-1;i++){
        for(int j=0;j<=i;j++){
            if(j==0){a[i][j]=1;}else{
    a[i][j]=a[i-1][j]+a[i-1][j-1];}
        cout<<a[i][j]<<' ';
        }
        cout<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务