简体   繁体   English

integer的序列(升序或降序)

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

A sequence of whole numbers, check if it is ordered true (in ascending or descending order), otherwise it is false .一个整数序列,检查它的排序是否为true (按升序或降序),否则为false If a number has the same value as the number below, it will not break the order.如果一个数字与下面的数字具有相同的值,它不会破坏顺序。 The sequence ends with 0 .该序列以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
--------------------------------

I need help, I've been trying for days... I really appreciate any help...我需要帮助,我已经尝试了好几天......我真的很感谢任何帮助......

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) {
            }
        }
    }
}

I will not tell you the code as that would not be of much help but I can help with the approach you need to take.我不会告诉你代码,因为那不会有太大帮助,但我可以帮助你采取需要采取的方法。

  1. With first 2 inputs assess if the pattern will be increasing or decreasing.使用前 2 个输入评估模式是增加还是减少。
  2. Then with the pattern check if the number is always = or less/greater than the number然后使用模式检查数字是否总是=或小于/大于数字
  3. Check for the last value.检查最后一个值。 It must be 0 but it may or may not be according to pattern (in some cases it may come as the correct pattern itself confusing if it is a valid number or end of list)它必须为 0,但它可能会或可能不会根据模式(在某些情况下,如果它是有效数字或列表末尾,它可能会因为正确的模式本身而令人困惑)
  4. If last number is not 0 then the output should be false.如果最后一个数字不是 0,那么 output 应该是假的。

You need to change your code like this:您需要像这样更改代码:

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;
    }
}

You can put this in a method and return false from the else and return true from the end of the method.您可以将它放在一个方法中并从else return false并从方法末尾return true

I assume 0 can't appear anywhere but at the end.我假设 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;
}

Test:测试:

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: 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. Based on the first two inputs (you will need a counter variable eg count ), decide whether the remaining numbers should be in ascending order or in descending order.根据前两个输入(您将需要一个计数器变量,例如count ),决定剩余的数字是升序还是降序。 You can use a boolean variable eg asc to store this result ie if the second number is greater than the first number, the value of asc will be true ;您可以使用boolean变量,例如asc来存储此结果,即如果第二个数字大于第一个数字,则asc的值为true otherwise, false .否则, false
  2. Once you have decided the value of asc from the first two numbers, you need to check if the next number follows this pattern or not.从前两个数字中确定asc的值后,您需要检查下一个数字是否遵循此模式。 If the next number doesn't follow the pattern, print false and break the processing.如果下一个数字不符合模式,则打印false并中断处理。
  3. For every number that the scanner reads, you also need to check if it is 0 .对于扫描仪读取的每个数字,您还需要检查它是否为0 If yes, print true and break the processing.如果是,则打印true并中断处理。 Also, since your requirement mentions, "If a number has the same value as the number below, it will not break the order."另外,由于您的要求提到, “如果一个数字与下面的数字具有相同的值,它不会破坏顺序。” , simply continue when the number read by the scanner has the same value as the last read number. ,当扫描仪读取的数字与上次读取的数字具有相同的值时,只需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;
            }
        }
    }
}

A sample run:示例运行:

9 8 7 6 5 4 3 2 1 0
true

Another sample run:另一个示例运行:

1 2 3 3 9 0
true

Another sample run:另一个示例运行:

1 2 5 5 2 3 0
false

Another sample run:另一个示例运行:

9 9 8 0
true

Another sample run:另一个示例运行:

5 5 6 0
true

The above code is failing on one test condition.上面的代码在一个测试条件下失败。 Test Condition: 4 4 1 2 3 0测试条件:4 4 1 2 3 0

Minor change needed in the above code ie, if (i == j && i<j) So the code will look like:上面代码中需要做的小改动,即 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