一个整数序列,检查它是否为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) {
}
}
}
}
我不会告诉您代码,因为这不会有多大帮助,但我可以帮助您采取所需的方法。
- 通过前两个输入,评估模式是否会增加或减少
- 然后用模式检查数字是否总是=或小于/大于数字
- 检查最后一个值。它必须是0,但根据模式可能是也可能不是(在某些情况下,如果它是一个有效的数字或列表末尾,它可能是正确的模式本身,这会让人感到困惑(
- 如果最后一个数字不是0,那么输出应该是false
我假设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)));
}
输出:
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
您需要这样更改代码:
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
。
- 根据前两个输入(您需要一个计数器变量,例如
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
上述代码中需要进行细微更改,即如果(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;
}
}
}
}