My Personal Solution using two dimensional array
This is my c form code.
#include <stdio.h>
#define N 20 // dimension
int main() {
int yTri[N][N] = {0};
for(int row = 0; row < N; ++row){
for(int col=0; col<= row; ++col){
if(col==0){
yTri[row][col] = 1; // the first element in each row set to 1
} else {
yTri[row][col] = yTri[row - 1 ][col - 1] + yTri[row - 1 ][col] ; // the previous row and column addition to produce the current number
}
printf("%4d", yTri[row][col]);
}
printf("\n");
}
return 0;
}
本文介绍了一种使用二维数组来实现杨辉三角的方法,并提供了一个C语言代码示例。该程序通过简单的循环结构填充数组并打印出杨辉三角形。
995

被折叠的 条评论
为什么被折叠?



