#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k;
int line;
int *prev, *next;
printf("輸入要查看楊輝三角的行數(大於2):");
scanf("%d",&line);
if(line < 2)
{
printf("行數小於2,Goodbye!n");
exit(1);
}
for(i=1; i<=line; i++) //前兩行的打印
printf(" ");
printf("%6dn",1);
for(i=1; i<=line-1; i++)
printf(" ");
printf("%6d%6dn",1,1);
prev = malloc(2*sizeof(int));
prev[0] = 1;
prev[1] = 1;
for(i=3; i<=line; i++) //從第三行開始打印
{
next = malloc(i*sizeof(int));
next[0] = 1;
next[i-1] = 1;
for(j=line; j>=i; j--) //外部空格
{
printf(" ");
}
printf("%6d",1);
for(k=2; k<i; k++) //數字
{
next[k-1] = prev[k-2] + prev[k-1];
printf("%6d",next[k-1]);
}
}
printf("%6dn",1);
free(prev);
prev = next;
}
free(next);
return 0;
}