在屏幕上打印杨辉三角。

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

int main(){
	int triangle[10][10] = { 0 },col=0,row=0,i,j;
	for (col = 0; col < 10; col++){
		for (row = 0; row < 10; row++){
			triangle[row][0] = 1;
			triangle[row][row] = 1;
			if (col>0 && row>0){
				i = triangle[row - 1][col - 1];
				j = triangle[row-1][col];
				triangle[row][col] = i + j;
			}
		}
	}
	for (row = 0; row < 10; row++){
		for (col = 0; col <= row; col++){
			printf("%d  ", triangle[row][col]);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43755544/article/details/85640567