/*
巴斯卡三角形
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
#include <stdio.h>
int main(int argc, const char * argv[]) {
int num = 0;
printf("请输入要打印的层数:\n");
scanf("%d",&num);
int arr[num][num];
for (int i = 0 ; i < num ; i++) {
arr[i][0] = 1;
for( int j = 0; j <= i ;j++){
arr[j][j] = 1;
}
}
for (int i = 0; i < num; i++) {
for (int j = 1; j < i; j++) {
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
for (int i = 0 ; i < num; i++) {
for (int j =0 ; j <= num - i; j++) {
printf(" ");
}
for (int j =0 ; j <= i; j++) {
printf("%2d ",arr[i][j]);
}
printf("\n");
}
}
|
|