Blue Bridge Cup - Basic Training 05-- Pascal's Triangle

Pascal's Triangle:

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 contains a number n.

Output format: the first n rows of the output Triangle. Each row from the first row are sequentially output number, using one intermediate spaces. Please do not print extra spaces in front.

Input Sample
4
Sample Output
. 1
. 1. 1
. 1 2. 1
. 1. 1. 3. 3

And the data size convention: 1 <= n <= 34.

code show as below:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int i, j, n;
	int a[34][34] ;
	
	scanf("%d",&n);
	a[0][0]=1;
	a[1][0]=1;
	a[1][1]=1;
	printf("1\n1 1\n");				//输出前面三个1;
	for(i=2; i<n; i++){
		a[i][0]=1;					//每一行的第一个都是1;
		printf("1 ");
		for(j=1; j<i+1; j++){
			a[i][j] = a[i-1][j-1] + a[i-1][j];	
			if(j==i){
				a[i][j]=1;
			}
			printf("%d ",a[i][j]);		
		}
		printf("\n");
	}
	return 0;
}

You still have to go a little debugging to see where there are problems, and then change it back, so be it -
quickly wrote a, come on!

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/88764118