#include <stdio.h>
#define LEN 40 //数组大小
void show(int n[], inti);
void calculate(int n[], int h, int max);
int main() {
int n[LEN] = {0};
int max;//行
printf("输入行数:");
scanf("%d",&max);
n[max] = 1;
int i = 0;
while( i < max ) {
if( max%2 != 0 ) //如果行是单数
show(n, (i+1)%2);
else
show(n, i%2);
n[max-i-1] = 1;
n[max+i+1] = 1;
calculate(n, i, max);
i++;
}
return 0;
}
void calculate(int n[], int h, int max) {
int start = max - h - 1;
int end = max + h + 1;
while(start < end) {
n[start+1] = n[start] + n[start+2];
start += 2;
}
}
void show(int n[], int i) {
if( i%2 != 0 )
printf(" ");
while( i < LEN ) {
if( n[i] != 0 )
printf("%-4d",n[i]);
else
printf(" ");
i += 2;
}
printf("\n");
}
可根据输出行数调节数组大小。
运行结果: