简体   繁体   中英

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

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. 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)
  4. If last number is not 0 then the output should be false.

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.

I assume 0 can't appear anywhere but at the end.

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:

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. 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 ; otherwise, 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. If the next number doesn't follow the pattern, print false and break the processing.
  3. For every number that the scanner reads, you also need to check if it is 0 . If yes, print true and break the processing. 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.
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

Minor change needed in the above code ie, if (i == j && i<j) So the code will look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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