簡體   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