Check ordered sequence (in ascending or descending order)

Asked

Viewed 397 times

-2

A sequence of integer numbers, check that it is ordered true (in ascending or descending order), otherwise false. If a number has the same value as the number below, it will not break the order. The sequence ends with 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) {
            }
        }
    }
}

1 answer

0


According to the help I received from Arvind Kumar Avinash, follows the explanation of the solution I learned from the answer :D

  1. Based on the first two entries (you need a counter variable, for example Count), decide whether the remaining numbers should be in ascending or descending order. You can use a variable boolean, for example, asc to store this result, that is, if the second number is greater than the first, the value of asc will be true; otherwise false,
  2. After deciding the value of asc first two numbers, it is necessary to verify whether the next number follows this pattern or not. If the next number does not follow the pattern, print false and stop processing.
  3. For every number the scanner reads, you also need to check if it is 0. If yes, print true and stop processing. Also, as the 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 number read.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.