Problem Description
Triangle known as Pascal triangle, its first row i + 1 is (a + b) i-expanding coefficient.
It is an important property: triangles each number equal to its two shoulders numbers together.
We are given below of the first four rows Triangle:
1
1 1
1 2 1
1 3 3 1
Given n, the first n rows outputs it.
Input format
input containing a number n.
Output format
output of the first n rows Triangle. Each row from the first row are sequentially output number, using one intermediate spaces. Please do not print extra spaces in front.
Sample input
4
Sample Output
1
1 1
1 2 1
1 3 3 1
Data size and convention
1 <= n <= 34。
The complete code:
C Language:
#include<stdio.h>
int a[105][105];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
a[i][0]=1; //每一行第一个数和最后一个数都是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(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(j!=i)
{
printf("%d ",a[i][j]);
}
else
{
printf("%d\n",a[i][j]);
}
}
}
return 0;
}
C++:
#include<iostream>
using namespace std;
int a[105][105];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
a[i][0]=1; //每一行第一个数和最后一个数都是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(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(j!=i)
{
cout<<a[i][j]<<" ";
}
else
{
cout<<a[i][j]<<endl;
}
}
}
return 0;
}