蓝桥杯 基础练习 杨辉三角形 (vip)

版权声明:(整理不易,如本文对您有益,请为我点赞吧!)本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/Qi2456/article/details/88291174

基础练习 杨辉三角形  

代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[110],b[110];
    if(n==1)
    {
        cout<<1;
    }
    else if(n==2)
    {
        cout<<1<<endl;
        cout<<1<<" "<<1;
    }
    else
    {
        cout<<1<<endl;
        cout<<1<<" "<<1<<endl;
        a[0]=1,a[1]=1;
        for(int i=3;i<=n;i++)
        {

            b[0]=1;b[i-1]=1;
            for(int j=0;j<i-2;j++)
            {
                b[j+1]=a[j]+a[j+1];
            }
            for(int j=0;j<i;j++)
            {
                if(j==0)
                {
                    cout<<b[j];
                    a[j]=b[j];
                }
                else
                {
                    cout<<" "<<b[j];
                    a[j]=b[j];
                }
            }
            cout<<endl;
        }

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Qi2456/article/details/88291174