1130: Pascal's Triangle
Title Description
Remember when high school learned of Pascal's triangle? Specific definition is not described here, you can refer to the following pattern:
. 1
. 1. 1
. 1. 1 2
. 1. 3. 3. 1
. 1. 4. 6. 4. 1
. 1. 5. 1. 5 10 10
Entry
Input contains only a positive integer n (1 <= n <= 30), Pascal's triangle represents the number of layers to be output.
Export
Pascal's triangle outputs a corresponding number of layers, each layer between integers separated by a space.
Sample input Copy
4
Sample output Copy
1
1 1
1 2 1
1 3 3 1
C
#include<stdio.h>
int main()
{
int n,i,j,a[31][31];
scanf("%d",&n);
for(i=0;i<n;i++)
a[i][0]=1,a[i][i]=1;
for(i=0;i<n;i++)
for(j=0;j<=i;j++){
if(i==j || j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d%c",a[i][j],j==i?'\n':' ');
}
return 0;
}