题解 | #有序序列判断#
有序序列判断
http://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
int index = 0;
while(scan.hasNextInt()) {
arr[index++] = scan.nextInt();
}
int[] copyArr = new int[n];
System.arraycopy(arr, 0, copyArr, 0, n);
Arrays.sort(copyArr);
int[] copyArrDaoxu = new int[n];
for(int i = 0; i < n; i++) {
copyArrDaoxu[i] = copyArr[n - i - 1];
}
if(Arrays.equals(arr, copyArr) || Arrays.equals(arr, copyArrDaoxu)) {
System.out.println("sorted");
} else {
System.out.println("unsorted");
}
}
}
Java基础练习题 文章被收录于专栏
都是一些基础的语法题目,每天可以刷几道。