繁体   English   中英

integer的序列(升序或降序)

[英]Sequence of integer (in ascending or descending order)

一个整数序列,检查它的排序是否为true (按升序或降序),否则为false 如果一个数字与下面的数字具有相同的值,它不会破坏顺序。 该序列以0结束。

Sample Input 1: 9 8 7 6 5 4 3 2 1 0
Sample Output 1:true
--------------------------------
Sample Input 2: 1 2 3 3 9 0
Sample Output 2:true
--------------------------------
Sample Input 3: 1 2 5 5 2 3 0
Sample Output 3: false
--------------------------------

我需要帮助,我已经尝试了好几天......我真的很感谢任何帮助......

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int a = 0;
        while (s.hasNextInt()) {
            int i = s.nextInt();
            a = i;
            if (i < a) {
                if (i < a) {
                    System.out.println("true");
                } else if (i > a) {
                    System.out.println("false");
                    break;
                }
            } else if (i > a) {
                if (i > a) {
                    System.out.println("true");
                } else if (i < a) {
                    System.out.println("false");
                    break;
                }
            } else if (i == a) {
            }
        }
    }
}

我不会告诉你代码,因为那不会有太大帮助,但我可以帮助你采取需要采取的方法。

  1. 使用前 2 个输入评估模式是增加还是减少。
  2. 然后使用模式检查数字是否总是=或小于/大于数字
  3. 检查最后一个值。 它必须为 0,但它可能会或可能不会根据模式(在某些情况下,如果它是有效数字或列表末尾,它可能会因为正确的模式本身而令人困惑)
  4. 如果最后一个数字不是 0,那么 output 应该是假的。

您需要像这样更改代码:

Scanner sc = new Scanner(System.in);

int prev = sc.nextInt();
int curr = sc.nextInt();

while (sc.hasNextInt() && prev == curr) {
    prev = curr;
    curr = sc.nextInt();
}

boolean flag = prev < curr;

while (sc.hasNextInt()) {
    prev = curr;
    curr = sc.nextInt();

    if (prev < curr && flag) {
        System.out.println("Ascending");
    } else if (prev > curr && !flag) {
        System.out.println("Descending");
    } else if (prev == curr) {
        System.out.println("Equal");
    } else {
        System.out.println("Not sorted");
        break;
    }
}

您可以将它放在一个方法中并从else return false并从方法末尾return true

我假设 0 不能出现在任何地方,但在最后。

static boolean ordered(Scanner s)
{
    int curr, prev;
    curr = prev = s.nextInt();

    while(s.hasNextInt() && (curr = s.nextInt()) == prev);

    if(curr < prev)
        while(s.hasNextInt() && (curr = s.nextInt()) <= prev) prev = curr;
    else
        while(s.hasNextInt() && (curr = s.nextInt()) >= prev) prev = curr;

    return curr == 0;
}

测试:

public static void main(String[] args)
{
    test("0");
    test("1 0");
    test("1 1 0");
    test("9 9 8 7 6 6 5 4 3 2 1 0");
    test("1 1 2 3 3 3 9 0");
    test("1 2 5 5 2 3 0");
    test("9 8 7 6 7 8 9 0");        
}

static void test(String str)
{
    System.out.format("%s : %b%n", str, ordered(new Scanner(str)));
}

Output:

0 : true
1 0 : true
1 1 0 : true
9 9 8 7 6 6 5 4 3 2 1 0 : true
1 1 2 3 3 3 9 0 : true
1 2 5 5 2 3 0 : false
9 8 7 6 7 8 9 0 : false
  1. 根据前两个输入(您将需要一个计数器变量,例如count ),决定剩余的数字是升序还是降序。 您可以使用boolean变量,例如asc来存储此结果,即如果第二个数字大于第一个数字,则asc的值为true 否则, false
  2. 从前两个数字中确定asc的值后,您需要检查下一个数字是否遵循此模式。 如果下一个数字不符合模式,则打印false并中断处理。
  3. 对于扫描仪读取的每个数字,您还需要检查它是否为0 如果是,则打印true并中断处理。 另外,由于您的要求提到, “如果一个数字与下面的数字具有相同的值,它不会破坏顺序。” ,当扫描仪读取的数字与上次读取的数字具有相同的值时,只需continue
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        boolean asc = true;
        int i = 0, j = 0, count = 0;
        while (true) {
            if (count < 2) {
                i = s.nextInt();
                if (i == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            } else {
                // Store the last input to `i` and read a new number into `j`
                i = j;
                j = s.nextInt();

                // Continue if the new number has the same value as the last read number
                if (i == j) {
                    continue;
                }

                if (j == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            }
            if (count <= 2) {
                j = s.nextInt();

                // Continue if the new number has the same value as the last read number
                if (i == j) {
                    continue;
                }

                if (j == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            }

            // Based on the first two inputs decide whether the remaining numbers should be
            // in ascending order or in descending order.
            if (count == 2 && j < i) {
                asc = false;
            }

            // Check if the next number (i.e. the value of `j`) follows this pattern or not
            if ((asc == true && j < i) || (asc == false && j > i)) {
                System.out.println(false);
                break;
            }
        }
    }
}

示例运行:

9 8 7 6 5 4 3 2 1 0
true

另一个示例运行:

1 2 3 3 9 0
true

另一个示例运行:

1 2 5 5 2 3 0
false

另一个示例运行:

9 9 8 0
true

另一个示例运行:

5 5 6 0
true

上面的代码在一个测试条件下失败。 测试条件:4 4 1 2 3 0

上面代码中需要做的小改动,即 if (i == j && i<j) 所以代码看起来像:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        boolean asc = true;
        int i = 0, j = 0, count = 0;
        while (true) {
            if (count < 2) {
                i = s.nextInt();
                if (i == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            } else {

                i = j;
                j = s.nextInt();

                if (i == j) {
                    continue;
                }

                if (j == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            }
            if (count <= 2) {
                j = s.nextInt();
 
                if (i == j && i < j) {
                    continue;
                }

                if (j == 0) {
                    System.out.println(true);
                    break;
                }
                count++;
            }

            if (count == 2 && j < i) {
                asc = false;
            }
            if ((asc == true && j < i) || (asc == false && j > i)) {
                System.out.println(false);
                break;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM