Description
杨辉三角,又称贾宪三角形,帕斯卡三角形,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。
下图的表在我国南宋数学家杨辉1261年所著的《详解九章算法》一书里就出现了。其中下一层的每一项都是由上一层连边的两项相加得来。
Input
输入多组,每组一个正整数n<14,至EOF结束。
Output
每组输入对应输出n层的杨辉三角,每一项占4个字符,除了每行开始需要填充空格之外,任意两项之间不要有多余的空格。
两组输出之间用一个空行分开,最后一组数据之后不要添加空行。
输出详细格式见sample。
Sample Input
7 5
Sample Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
#define MAX_SIZE 10005
int main() {
int n = 0;
while (scanf("%d", &n) != EOF) {
int j, x = 1;
int i, s;
int a[16]={};
int b[16] = {1, 1};
for (i = 1; i <= n; ++i) {
for (j = 0; j < (n - i); j++) {
printf(" ");
}
if (n > 2) {
for (s = 1; s < x; s++)
{
b[s] = a[s - 1] + a[s];
}
}
for (i = 0; i < x; i++) {
a[i] = b[i];
printf("%4d", a[i]);
}
x++;
printf("\n");
}
x = 0;
printf("\n");
}
}