打印杨辉三角的另一种简单方法

来源:互联网 发布:plsql导出sql脚本乱码 编辑:程序博客网 时间:2024/05/16 05:43

#include<iostream>
using namespace std;
int main()
{
 int k,a[100][100],m=1,n=1,s,x;
 a[1][0]=1;
 cout<<"请输入要打印的杨辉三角的行数:"<<endl;
 cin>>k;
 for(int i=0;i<=(k-1);i++)
  a[0][i]=1;
 cout<<"1"<<endl;
 cout<<"1 1"<<endl;
 for(int y=3;y<=k;y++)
 {
  x=1;
  cout<<"1"<<" ";
  while(x<(y-1))
  {
   s=0;
   for(int j=0;j<=n;j++)
    s=s+a[m-1][j];
   a[m][n]=s;
   x++;
   cout<<a[m][n]<<" ";
   m++;
   n--;
   if(n==0)
    a[m][n]=1;
  }
  cout<<"1"<<endl;
  m=1;
  n=y-1;
 }
 return 0;
}

 

原创粉丝点击