【问题标题】:How can i determine the formula for determining the first number in Java's loop array for floyd's triangle?如何确定用于确定弗洛伊德三角形的 Java 循环数组中第一个数字的公式?
【发布时间】:2020-03-16 00:52:55
【问题描述】:

嗨,我有 java 循环问题。

所以我试图弄清楚如何通过输入三角形的高度来确定弗洛伊德三角形循环中的第一个数字(在模式的顶部)。

注意:只输入高度来确定第一个数字,最后一个数字应固定为1。

例如:

Enter the height: 5

The first number is: 15

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

另一个是

Enter the height: 6

The first number is: 21

21
20 19
18 17 16
15 14 13 12
11 10 9  8  7
6  5  4  3  2  1 

我已经弄清楚了如何进行模式和值的递减,但我似乎无法弄清楚第一个数字。我一直在试图弄清楚顺序,但它仍然让我感到困惑,因为我还是 java 新手。

这是我的代码:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {

        int n;
        int startingnumber = ;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the height of the triangle: ");
        n = input.nextInt();
        System.out.print("The first number is "+startingnumber);

        for(int i =1; i<=n; i++){

            for(int j =1; j<=i; j++){
                System.out.print(startingnumber);
                startingnumber--;
            }

            System.out.println();
        }


    }
}

代码还没写完,因为我不知道公式:(

如果我能找到任何帮助,我将不胜感激。谢谢!

【问题讨论】:

    标签: java loops for-loop while-loop


    【解决方案1】:

    这个数学问题是Triangular number,这里是visual demonstration

    S1 = 1
    S2 = 1 + 2
    S3 = 1 + 2 + 3
    ...
    Sn = 1 + 2 + 3 + ... + n
    
    => 1 + 2 + 3 + ... + n = n * (n + 1) / 2
    

    也可以看看System.out.printf

    public static void main(String[] args) {
        int n;
        int startingnumber;
    
        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter the height of the triangle: ");
        n = input.nextInt();
        startingnumber = n * (n + 1) / 2;
        System.out.println("The first number is " + startingnumber);
    
        for (int i = 1; i <= n; i++) {
    
            for (int j = 1; j <= i; j++) {
                System.out.printf("%3d ", startingnumber);
                startingnumber--;
            }
    
            System.out.println();
        }
    }
    

    输出

    Enter the height of the triangle: 6
    The first number is 21
     21 
     20  19 
     18  17  16 
     15  14  13  12 
     11  10   9   8   7 
      6   5   4   3   2   1 
    

    【讨论】:

    • 成功了!但是你是怎么计算出这个公式的呢?在过去的 3 个小时里,我一直试图解决这个问题,但你立刻就明白了。有什么方法可以得到这样的数学公式吗?
    • @Brian 通常你会从基本的例子S1 = 1S2 = 1 + 2 开始,然后尝试推导出一个通用公式Sn = 1 + 2 + 2 + .. + nn * (n + 1) / 2
    【解决方案2】:

    解决这类问题的方法是找到数学关系。在这种情况下,您知道(当输入为 6 时)高度为 6。您还知道,在每一行,您的数字比后面的数字少一个。最下面有6个,和高度一样。

    因此需要做6+5+4+3+2+1才能得到起始编号。

    现在公式化为通用解决方案:n+(n-1)+((n-1)-1)..+1。

    一个可能的实现是:

    System.out.print("Enter the height of the triangle: ");
    n = input.nextInt();
    int startingNumber = 0;
    for (int i=n;i>0;i--) startingNumber+=i;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2013-02-06
      • 2018-03-19
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多