1

       1     1

       1     2     1

       1     3     3      1

       .   .............     .

      根据杨辉三角的性质可知:

      1)每一行的第一列的数值都为1。

      2)每一行的最后一列的数值都为1。

      3)其它行列对应的数值等于上一行,上一列对应的数与上一行,对应列的数之和。

      代码如下:       

public void PrintYangHuiTriangle(int count)
{
      int[][] arrInt=new int[count][];
      for(int i=0;i<arrInt.Length;i++)
      {
            arrInt[i]=new int[i+1];
            for(int j=0;j<arrInt[i].Length;j++)
            {
                   if(i==0||i==j)
                   {
                        arrInt[i][j]=1;
                    }
                    else
                    {
                        arrInt[i][j]=arrInt[i-1][j-1]+arrInt[i-1][j];
                    }
             }
       }
       for(int i=0;i<arrInt.Length;i++)
       {
             for(int j=0;j<arrInt[i].Length;j++)
             {
                    Console.Write("{0}\t",arrInt[i][j]);
                    if(i==j)
                    {
                        Console.WriteLine();  
                     }
             }  
       }
}

  

 posted on 2014-06-23 20:25  会飞的金鱼  阅读(114)  评论(0)    收藏  举报