c++杨辉三角问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 19:37:25
运行这个程序,得到的结果如下,请问一下是那个语句出现了问题,要怎样修改。
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
const int maxn=10;
int a[maxn][maxn];
for(int i=0;i<maxn;i++)
{
a[i][0]=1;
a[i][i]=1;
for(int j=1;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<maxn;i++)
{
cout<<setw((maxn-i)*5/2);
for(int j=1;j<=i;j++)
cout<<a[i][j]<<setw(5);
cout<<endl;
}
}

1
2 1
3 3 1
4 6 4 1
5 10 10 5 1
6 15 20 15 6 1
7 21 35 35 21 7 1
8 28 56 70 56 28 8 1
9 36 84 126 126 84 36 9 1

for(int i=0;i<maxn;i++)
{
cout<<setw((maxn-i)*5/2);
for(int j=0;j<=i;j++) ### j=0开始
cout<<a[i][j]<<setw(5);
cout<<endl;
}

#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
int a[100][100],i,j,k,n;
do{ //控制打印行数
cout<<"请输入要打印的行数(<=13):\n";
cin>>n;
if(n>13)cout<<"error!行数超出范围!\n";
}while(n>13);
for(i=0;i<n;i++) //给每个数赋值
for(j=0;j<=i;j++)
{
a[j][j]=1;
a[i][0]=1;
if(i>1&&j>0&&i>j)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<n;i++)
{
for(k=0;k<=35-3*i;k++) //打印空格
cout<<' ';