标签:杨辉三角
使用动态内存打印任意阶杨辉三角: #include<stdio.h> #include<stdlib.h> void init_arr(int **p,intline) { for (int i= 0; i<line; i++) { p[i][0] = 1; } for (int i = 1; i<line; i++) { for(int col= 1; col <= i; col++) { p[i][col] = p [i - 1][col] + p[i - 1][col - 1]; } } } void input(int **p,int line) { for (int i= 0; i<line; i++) { for (k = 0; k <=line - i; k++) printf( " "); for (int col= 0; col <= i; col++) { printf( "%2d ",p [i][col]); } printf( "\n"); } } int main() { int line = 0; printf( "输入要打印的行数\n" ); scanf( "%d", &line); int **p = (int **)calloc(line, 4); if (p == NULL ) { printf( "out of memory\n"); exit( EXIT_FAILURE); } for (int i = 0; i < line; i++) { p[i] = ( int *)calloc(line, 4); if (p[i] == NULL ) { printf( "out of memory\n"); exit( EXIT_FAILURE); } } init_arr(p,line); input(p,line); for (int i = 0; i < line; i++) { free(p[i]); } free(p); system( "pause"); return 0; }
本文出自 “11132019” 博客,请务必保留此出处http://11142019.blog.51cto.com/11132019/1760094
标签:杨辉三角
原文地址:http://11142019.blog.51cto.com/11132019/1760094