标签:
#include<stdio.h> #include<stdlib.h> int** fmalloc(int n){ int** array; int i; array = (int** )malloc(sizeof(int*) * n); for(i=0; i<n; ++i){ array[i] = (int*)malloc(sizeof(int*) * (i+1)); } return array; } int main() { int n; int i,j; printf("请输入杨辉三角的行数:"); scanf("%d",&n); int** array = fmalloc(n); for(i = 0; i<n; ++i){ for(j=0; j<=i; ++j){ if(i==0 || i==1) array[i][j]=1; else if(j == 0 || j==i) array[i][j] = 1; else array[i][j] = array[i-1][j-1] + array[i-1][j]; } } for(i = 0; i<n; ++i){ printf("\n"); for(j=0; j<=i; ++j){ printf("%d\t",array[i][j]); } } printf("\n"); for(i=0; i<n; ++i){ free(array[i]); } free(array); return 0; }
标签:
原文地址:http://www.cnblogs.com/mingbujian/p/4639655.html