题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
int x = in.nextInt();
a[i] = x;
}
int q = 0;
for (int i = 0; i < n; i++) {
if (i < n - 1) {
if (a[1] < a[0]) {
for(int j=i+1;j<n;j++){
if(a[i]<a[j]){
break;
}else{
q += 1;
}
}
} else {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
break;
} else {
q += 1;
}
}
}
}
}
if (q == (n * (n - 1)) / 2) {
System.out.println("sorted");
} else {
System.out.println("unsorted");
}
}
}
查看9道真题和解析