[英]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
.一个整数序列,检查它的排序是否为
true
(按升序或降序),否则为false
。 If a number has the same value as the number below, it will not break the order.如果一个数字与下面的数字具有相同的值,它不会破坏顺序。 The sequence ends with
0
.该序列以
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.我不会告诉你代码,因为那不会有太大帮助,但我可以帮助你采取需要采取的方法。
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.您可以将它放在一个方法中并从
else
return false
并从方法末尾return true
。
I assume 0 can't appear anywhere but at the end.我假设 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;
}
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: 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
), decide whether the remaining numbers should be in ascending order or in descending order.count
),决定剩余的数字是升序还是降序。 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
;boolean
变量,例如asc
来存储此结果,即如果第二个数字大于第一个数字,则asc
的值为true
; otherwise, false
.false
。asc
from the first two numbers, you need to check if the next number follows this pattern or not.asc
的值后,您需要检查下一个数字是否遵循此模式。 If the next number doesn't follow the pattern, print false
and break the processing.false
并中断处理。0
.0
。 If yes, print true
and break the processing.true
并中断处理。 Also, since your requirement mentions, "If a number has the same value as the number below, it will not break the order."continue
when the number read by the scanner has the same value as the last read number. 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;
}
}
}
}
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
测试条件: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:上面代码中需要做的小改动,即 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.