zoukankan      html  css  js  c++  java
  • 打印杨辉三角形

    以等腰三角形形式打印杨辉三角。

    比如:

                 1
              1    1
            1    2    1
         1    3    3    1
       1    4    6    4    1


    代码例如以下:

    void print_line(int * line, int ln, int line_width)
    {
             int wscount = (line_width-((ln+1)*4+ln))/2;
             for(int i=0; i<wscount; ++i)
                    printf( " ");
             for(int i=0; i<ln; ++i)
            {
                    printf( "%4d ", line[i]);
            }
            printf( "%4d ", line[ln]);
    }

    // calculate next line of yanghui triangle
    void next_line(int * line, int ln)
    {
             int last_col = line[0];
             for(int col=1; col<=ln; ++col)
            {
                     int tmp = line[col];
                    line[col] += last_col;
                    last_col = tmp;
            }
            line[ln+1] = 1;
    }

    void print_yanghui(int n)
    {
             if(n<=0)
                     return;
             // allocate one more int to avoid that next_line function writes after the end.
             int* line = new int[n+1];
            line[0] = 1;
             int line_width = n*4+(n-1);
             for(int ln=0; ln<n; ++ln)
            {
                    print_line(line, ln, line_width);
                    next_line(line, ln);
            }
             delete []line;
    }


    int main () {
             for(int i=0; i<12; ++i)
                    print_yanghui(i);
             return 0;
    }

  • 相关阅读:
    转载:AAC编解码概述
    转载:ADTS header
    wcf寄宿在iis上的跨域访问问题【不止是添加跨域文件】
    转 http 分析工具
    时间管理1
    关于silverlight和Wcf分布式部署注意问题(收藏夹)
    c#修改xml文件
    关于在线编辑的异常
    创业文摘5--从程序员转向企业家的10个建议
    silverlight 后台代码生成gridview
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7142357.html
Copyright © 2011-2022 走看看