C language: Yang Hui triangle

Yang Hui triangle

Enter an integer n less than 20, and output the Yang Hui triangle of n rows. The format is as follows:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
……

Note : there can be no spaces after the last number in each line

#include<stdio.h>
#define N 100
int a[N][N];
int main()
{
    
    	
	int i, j, n;
	scanf("%d", &n);
	
	for(i = 0;i < n;i++) //使数组的第一列都为1
	a[i][0] = 1;

	for(i = 1;i < n;i++) 
		for(j = 1;j <= i;j++)
		a[i][j] = a[i-1][j] + a[i-1][j-1];//杨辉三角形实现 

	for(i = 0;i < n;i++)   
	{
    
    
		for(j = 0;j <= i;j++)
		if(i == j) printf("%d\n", a[i][j]);
		else printf("%d ", a[i][j]);
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/m0_51354361/article/details/113574114