当前位置: 代码迷 >> Java相关 >> 杨辉三角
  详细解决方案

杨辉三角

热度:740   发布时间:2012-10-11 21:17:48.0
杨辉三角
class  yanghui
{
    public static void main(String[] args)
    {
        //定义数组;
        int arrlength=10;
        int [][] array=new int[arrlength][];
        for(int x=0;x<array.length;x++)
        {
          array[x]=new int[x+1];
          for(int y=0;y<x+1;y++)
            { if(x==0||y==0||x==y)
                { array[x][y]=1;}
                else
                {array[x][y]=array[x-1][y-1]+array[x-1][y];}
         
            }

        }

   for(int x=0;x<array.length;x++)
    {
       for(int i=0;i<9-i;i++)
        {System.out.print("\t");
       }
      for(int y=0;y<x+1;y++)
        {System.out.print(array[x][y]+"\t\t");}
       System.out.println();
   
   
   }

结果已经得出,但输出有点问题,请各位帮我解释一下,为什么加“\t”,会出问题
搜索更多相关的解决方案: public  杨辉三角  void  

----------------解决方案--------------------------------------------------------
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int arrlength = 10;
        int[][] array = new int[arrlength][];
        for (int x = 0; x < array.length; x++) {
            array[x] = new int[x + 1];
            for (int y = 0; y < x + 1; y++) {
                if (x == 0 || y == 0 || x == y) {
                    array[x][y] = 1;
                } else {
                    array[x][y] = array[x - 1][y - 1] + array[x - 1][y];
                }

            }

        }

        for (int x = 0; x < array.length; x++) {
            for (int i = 0; i < 9 - x; i++) {
                System.out.print("\t");
            }
            for (int y = 0; y < x + 1; y++) {
                System.out.print(array[x][y] + "\t\t");
            }
            System.out.println();
        }
    }


仔细 仔细

----------------解决方案--------------------------------------------------------