[C++] 纯文本查看 复制代码 #include<bits/stdc++.h>
using namespace std;
void yanghui(int n)//杨辉三角形
{
int a[100][100]={0};
for(int i=0;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
if(j==0)
{
a[i][j]=1;
}
else if(i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
printf("%5d",a[i][j]);
//cout<<setw(5)<<a[i][j];
}
cout<<endl;
}
}
void yanghui2(int n)//杨辉三角形
{
int a[100][100]={0};
for(int i=0;i<=n;i++)
{
for(int j=n-1-i;j<=n-1;j++)
{
if(j==n-1)
{
a[i][j]=1;
}
else if(j+i==n-1)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j]+a[i-1][j+1];
}
}
}
for(int i=0;i<=n-1;i++)
{
for(int j=0;j<=n-1;j++)
{
if(a[i][j]==0)
{
printf("%-4c",' ');
}
//printf("%5d",a[i][j]);
//cout<<setw(5)<<a[i][j];
}
cout<<endl;
}
}
|