[英]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) {
}
}
}
}
我不會告訴你代碼,因為那不會有太大幫助,但我可以幫助你采取需要采取的方法。
您需要像這樣更改代碼:
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
count
),決定剩余的數字是升序還是降序。 您可以使用boolean
變量,例如asc
來存儲此結果,即如果第二個數字大於第一個數字,則asc
的值為true
; 否則, false
。asc
的值后,您需要檢查下一個數字是否遵循此模式。 如果下一個數字不符合模式,則打印false
並中斷處理。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.