码迷,mamicode.com
首页 > 其他好文 > 详细

打印杨辉三角

时间:2016-04-04 21:18:00      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:杨辉三角

使用动态内存打印任意阶杨辉三角:
#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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!