zoukankan      html  css  js  c++  java
  • 算法实现三角形式输出C(n,k)

    Practice :

    The values of the combinations method used in the text are often displayed in the form of a triangle using the following arrangement:

    C(0,0)

    C(1,0)   C(1,1)

    C(2,0)   C(2,1)   C(2,2)

    C(3,0)   C(3,1)   C(3,2)   C(3,3)

    C(4,0)   C(4,1)   C(4,2)   C(4,3)   C(4,4)

    and so on.  This figure is called Pascal’s Triangle after the seventeenth-century French mathematician Blaise Pascal, who described it, even though it was known by Chinese mathematicians over 2000 years ago.  Pascal’s Triangle has the interesting property that every interior entry is the sum of the two entries above it.

      Write a Java Program  to display the first eight rows of Pascal’s Triangle.

    --------------------------------------------------------------------------------------------------------------------------------------------------

    Program:

     package chapter5;

     public class triangleCn {

       static int n=8;
       public void run(){ triangleCn(); }     //  调用方法triangleCn()

      public static String triangleCn(){
       
          for(int i=0;i<=n;i++){      //控制行数
        
          Combinations com1=new Combinations();    // 实例化Combinations对象

     
          for(int k=n;k>=i;k--){    //控制输出格式三角型
             System.out.print(" ");
          }


          for(int j=0;j<=i;j++){      //控制每行的输出的数字
            int num1=com1.Combinations(i, j);     //调用函数Combinations(int n, int k)
            System.out.print(" "+num1);
            }
          System.out.println();
           }
        return null;
        }
     
      }

    不断的总结,才能不断的提高;不断的思考,才能不断的进步!
  • 相关阅读:
    C#简单游戏外挂制作(以Warcraft Ⅲ为例)
    Push模式
    关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用
    更改SQL表的所有者
    Microsoft Visual Studio 2005中使用水晶报表(非常棒)
    简单介绍一下水晶报表的推与拉两种模式
    SQL函数之四舍五入(转)
    如何制作一个多栏报表
    ASP.NET dropdownlist绑定数据源两种方法
    PUSH模式样板招式
  • 原文地址:https://www.cnblogs.com/nzyjlr/p/2009207.html
Copyright © 2011-2022 走看看