微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反转递增三角形图案

如何解决反转递增三角形图案

我正在编写一些代码来创建一个如下所示的递增数字直角三角形:

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

我不确定如何让我的代码输出一个三角形到那个形状,因为我的代码输出相同的东西,除了倒置。

这是我的代码

public class Main {
    public static void main(String[] args) {
        int rows = 6;
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= i; ++j) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

我的猜测是,我不会增加某些值,而是减少它们,但是代码会运行无限的垃圾值,而不是我想要的。

解决方法

在打印每一行的数字之前需要打印一些空格,并且空格的数量应该根据行减少:

int rows = 6;

for (int i = 1; i <= rows; ++i) {
    for (int j = rows - i; j >= 1; j--) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

这个前缀可以使用 String::join + Collections::nCopies 构建:

System.out.print(String.join("",Collections.nCopies(rows - i,"  ")));

或者从 Java 11 开始,可以使用 String::repeat 替换此前缀:

System.out.print("  ".repeat(rows - i));
,

代替两个嵌套的 for 循环,您可以使用带有两个递增变量的单个 while 循环。迭代次数保持不变。

int n = 7,i = 0,j = 0;
while (i < n) {
    // element
    if (i + j >= n - 1) {
        // print an element
        System.out.print(i + j + 2 - n);
    } else {
        // print a whitespace
        System.out.print(" ");
    }
    // suffix
    if (j < n - 1) {
        // print a delimiter
        System.out.print(" ");
        j++;
    } else {
        // print a new line
        System.out.println();
        j = 0;
        i++;
    }
}

输出:

            1
          1 2
        1 2 3
      1 2 3 4
    1 2 3 4 5
  1 2 3 4 5 6
1 2 3 4 5 6 7

另见:Optimized Bubble Sort

,

您的方法几乎是正确的 - 使用两个嵌套的 for 循环,剩下的就是添加一个 if else 语句并计算坐标 sum {1}} 和 i

j

输出:

public static void main(String[] args) {
    int n = 6;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            System.out.print(" ");
            int sum = i + j;
            if (sum > n)
                System.out.print(sum - n);
            else
                System.out.print(" ");
        }
        System.out.println();
    }
}
,

在打印数字之前必须先打印空格以使三角形看起来倒置,空格的数量取决于您跳过的数字数量rows-i,因此您可以从i循环到rows 并在每次迭代中打印空间。

int rows = 6;
for (int i = 1; i <= rows; ++i) {
    for (int j = i; j < rows; j++) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

输出:

          1 
        1 2 
      1 2 3 
    1 2 3 4 
  1 2 3 4 5 
1 2 3 4 5 6 
,

您可以使用属性,对于包含 i 的行作为该行的最大行数,空格数可以计算为 2*(rows-i)。你可以像下面这样重写你的程序:

public class Main {
    public static void main(String[] args) {
        int rows = 6;
        for (int i = 1; i <= rows; ++i) {
            for (int nspaces = 0; nspaces < 2 * (rows - i); ++nspaces) {
                System.out.print(" ");
            }
            for (int j = i; j > 0; --j) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

输出:

          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 
,

您可以使用两个嵌套的 for 循环打印倒三角形,如下所示:

// the number of rows and the
// number of elements in each row 
int n = 6;
// iterating over rows with elements
for (int i = 0; i < n; i++) {
    // iterating over elements in a row
    for (int j = 0; j < n; j++) {
        // element
        if (i + j >= n - 1) {
            // print an element
            System.out.print(i + j + 2 - n);
        } else {
            // print a whitespace
            System.out.print(" ");
        }
        // suffix
        if (j < n - 1) {
            // print a delimiter
            System.out.print(" ");
        } else {
            // print a new line
            System.out.println();
        }
    }
}

输出:

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

另见:How to draw a staircase with Java?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。