Hangzhou Electric Oj brush title (2032)

Pascal's Triangle

Subject 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

Input

Input data comprising a plurality of test cases, each test case comprising input only a positive integer n (1 <= n <= 30), Pascal's triangle represents the number of layers to be output.

Output

Pascal's triangle corresponding to each input, output a corresponding number of layers, each layer between integer separated by a space, each of Pascal's triangle behind a blank line.

Sample Input

2 3

Sample Output

1 
1 1 

1 
1 1 
1 2 1

By the answer:

#include<stdio.h>
int main(){
	int n,i,j;
	int a[31][31];               //定义一个二维数组(最大为30层) 注意!
	while(~scanf("%d",&n)){
		for(i=1;i<=n;i++){       //每层第一个和最后一个元素为1 
			a[i][1]=1;
			a[i][i]=1;
		}
		for(i=3;i<=n;i++){              //除两边的数外其余的数均为上一层两数之和 
			for(j=2;j<=i-1;j++){
				a[i][j]=a[i-1][j-1]+a[i-1][j]; 
			}
		}
		for(i=1;i<=n;i++){              //数与数之间有一个间隔 
			printf("%d",a[i][1]);
			for(j=2;j<=i;j++){
				printf(" %d",a[i][j]);
			}
			printf("\n");
		}
		printf("\n");                //最后输出一个空行 
	}
	return 0;
}

 

Published 55 original articles · won praise 0 · Views 1007

Guess you like

Origin blog.csdn.net/ZhangShaoYan111/article/details/104168758