打印数字三角形

时间:2013-10-21 18:22:43

标签: java loops

CS学生在这里。我刚刚收到了关于循环的介绍,我不确定我是否理解它们。我试图打印一个数字三角形n,这样如果n = 4你得到这样的结果:

         4
      3  7
   2  6  9
1  5  8 10
相反,我会用以下的东西结束:

   4
3   5

我只想说我迷路了。这是我的代码:

void drawT3 (int n)
{
    int k = 1;
    int t = 1;
    for (int i=1;i<=n;i++)
    {  
        k = n;
        int j;
        for (j=1;j<=n-i;j++)
            System.out.print(" ");

        for (j=1;j<=t;j++)
        {
            System.out.printf("%3d",k);
            k += (n - j);
        }
        n--;
        t++;
        System.out.println();
    }
}

4 个答案:

答案 0 :(得分:2)

void printTriangle(int n)
{ 
    // build an auxiliary 2D array
    final int t[][] = new int[n][n];
    int i = 1;
    for (int s = n - 1; s <= 2 * (n - 1); s++)
    {
        for (int x = s - n + 1; x < n; x++)
        {
            t[x][s - x] = i++;
        }
    }
    // print the array
    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < n; x++)
        {
            if (t[x][y] > 0)
            {
                System.out.printf("%3d", t[x][y]);
            }
            else
            {
                System.out.printf("   ");
            }
        }
        System.out.println(); // start new line
    }
}
  1. 构建大小为n的辅助2D数组。
  2. 按照对角线的方式将数字放入数组中,从1到n。代码中的s代表x + y总和。每个对角线的总和是恒定的。在第一个对角线(最长的一个)中,总和等于n - 1。在第二个对角线和中,还有1个n。在最后的“对角线”(右下角)中,总和为2 * (n - 1)。这正是我们的循环:for (int s = n - 1; s <= 2 * (n - 1); s++)。得到总和x我们可以通过简单的减法y获得y = s - x
  3. 打印阵列。数组的每个单元格都用0(int的默认值)初始化。因此,如果单元格为零,我们只打印3个空格,以保留三角形的形状。
  4. PS。我的代码是为“教育目的”而编写的:)以简单的方式展示如何完成它。它没有针对速度和内存进行优化。

答案 1 :(得分:1)

int n=4,i,j,k,t;
for (i=n;i>=1;i--)
{  
    t=i;
    k=n;
    for(j=1;j<i;j++)
        System.out.printf("   ");  // for leading spaces

    System.out.printf("%3d",i);   // for first digit(or number) in each row (in your example these are 4,3,2,1)

    for(j=i;j<n;j++)
    {
        t+=k;   
        System.out.printf("%3d",t);
        k--;
    }
    System.out.print("\n");
}

输出: 对于n = 8

                      8
                   7 15
                6 14 21
             5 13 20 26
          4 12 19 25 30
       3 11 18 24 29 33
    2 10 17 23 28 32 35
 1  9 16 22 27 31 34 36

http://ideone.com/C1O1GS

根据您的需要在数字周围留出空间。

PS:我绝不会建议使用数组编写任何模式代码,除非它非常复杂。数组将使用额外的内存空间。

答案 2 :(得分:1)

public static void main(String[] args) {
    // TODO code application logic here
    triangle(4);
}

static public void triangle(int n){
    int x = 0;
    for (int i = n;i>0;i--){
        System.out.print(i + " ");
        x = i+n;
        for (int j=0;j<n-i;j++){
            System.out.print(x - j + " ");
            x = x + n -j;
        }
        System.out.println("");
    }
}

4的输出:

4

3 7

2 6 9

1 5 8 10

6的输出:

6

5 11

4 10 15

3 9 14 18

2 8 13 17 20

1 7 12 16 19 21

答案 3 :(得分:1)

观察到有很多方法可以打印出如上所述的数字三角形,例如,这里有两个,

// for n=5,
// 1  2  3  4  5
//    6  7  8  9
//      10 11 12
//         13 14
//            15

//             5
//          4  9
//       3  8 12
//    2  7 11 14
// 1  6 10 13 15

因为递归是 Fun!

class triangle
{
    //Use recursion,
    static int rowUR( int count, int start, int depth )
    {
        int ndx;
        if(count<=0) return start;
        //-depth?
        for (ndx=0;ndx<depth;ndx++)
        {
            System.out.print("   ");
        }
        //how many? 5-depth, 5,4,3,2,1
        for( ndx=0; ndx<count; ++ndx )
        {
            System.out.printf("%3d",start+ndx);
        }
        System.out.printf("\n");
        if( count>0 )
        {
            rowUR( count-1, ndx+start, depth+1 );
        }
        return ndx;
    }
    //Use recursion,
    static int rowLR( int count, int start, int depth )
    {
        int ndx, accum;
        if( start < count )
            rowLR( count, start+1, depth+1 );
        for( ndx=0; ndx<depth; ++ndx )
        {
            System.out.print("   ");
        }
        accum=start;
        //how many? 5-depth, 1,2,3,4,5
        for( ndx=0; ndx<(count-depth); ++ndx )
        {
            System.out.printf("%3d",accum);
            accum+=count-ndx;
        }
        System.out.printf("\n");
        return ndx;
    }
    public static void main(String[] args)
    {
        int count=4, depth=0, start=1;
        System.out.printf("rowUR\n");
        rowUR( count=5, start=1, depth=0 );
        System.out.printf("rowLL\n");
        rowLL( count=5, start=1, depth=0 );
    }
};
相关问题