输入一个整数序列,判断是否是有序序列,有序,指序列中的整数从小到大排序或者从大到小排序(相同元素也视为有序)。
数据范围: 序列中的值都满足
第一行输入一个整数N(3≤N≤50)。第二行输入N个整数,用空格分隔N个整数。
输出为一行,如果序列有序输出sorted,否则输出unsorted。
5 1 6 9 22 30
sorted
5 3 4 7 2 10
unsorted
5 1 1 1 1 1
sorted
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } int count1 = check1(nums, n); int count2 = check2(nums, n); if (count1 == n - 1 || count2 == n - 1) { System.out.println("sorted"); } else { System.out.println("unsorted"); } } //降序或相等的情况 public static int check1(int[] nums, int n) { int count1 = 0; for (int i = 0; i < n - 1; i++) { if (nums[i] >= nums[i + 1]) { count1++; } else { count1--; } } return count1; } //升序情况 public static int check2(int[] nums, int n) { int count2 = 0; for (int i = 0; i < n - 1; i++) { if (nums[i] < nums[i + 1]) { count2++; } else { count2--; } } return count2; } }
import java.util.Scanner; import java.util.Arrays; import java.util.Collections; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int[] num = new int[a]; for (int i = 0; i < a; i++) { num[i] = in.nextInt(); } int[] cNum1 = Arrays.copyOf(num, num.length); Arrays.sort(cNum1); int[] cNum2 = Arrays.copyOf(cNum1, cNum1.length); for(int i =0;i<cNum2.length/2;i++) { int tmp =cNum2[i]; cNum2[i]=cNum2[cNum2.length-1-i]; cNum2[cNum2.length-1-i]=tmp; } if(Arrays.equals(num, cNum1) || Arrays.equals(num, cNum2)) { System.out.println("sorted"); } else { System.out.println("unsorted"); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int N = in.nextInt(); int p1 = 1; int p2 = 100; boolean flag1 = true;//升序 boolean flag2 = true;//降序 for(int i=0;i<N;i++){ int v = in.nextInt(); if(flag1 && v<p1){ flag1=false; } if(flag2 &&v>p2){//降序 flag2=false; } p1=p2=v; } System.out.println((flag1||flag2)?"sorted":"unsorted"); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a=in.nextInt(); int[] arrA=new int[a]; int[] arrB=new int[a]; int countA=0; int countB=0; for(int i=0;i<arrA.length;i++){ arrA[i]=in.nextInt(); } for(int i=0;i<arrA.length;i++){ arrB[i]=arrA[i]; } Arrays.sort(arrB); for(int i=0;i<arrA.length;i++){ if(arrA[i]==arrB[i]){ countA++; } if(arrA[i]==arrB[arrA.length-i-1]){ countB++; } } if(countA==a||countB==a){ System.out.print("sorted"); }else{ System.out.print("unsorted"); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); List<Integer> list1=new ArrayList<>(); for(int i=0;i<n;i++) list1.add(sc.nextInt()); List<Integer> list2=new ArrayList<>(); for(Integer num:list1) list2.add(num); if(list1.get(0)>list1.get(list1.size()-1)) Collections.sort(list1,Comparator.reverseOrder()); else Collections.sort(list1); String type=""; for(int i=0;i<n;i++){ if(list1.get(i)!=list2.get(i)){ type="un"; break; } } System.out.printf("%ssorted",type); } }
import java.util.*; public class Main { //方法:冒泡排序(从小到大顺序) public static int[] maopao1(int[] arr) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } //方法:冒泡排序(从大到小顺序) public static int[] maopao2(int[] arr) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } //主程序:输入数组,调用冒泡,输出 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean q = true; int[] arr = new int[n]; //原数组 int[] arrmin = new int[n];//复制原数组,准备调用方法1 int[] arrmax = new int[n];//复制原数组,准备调用方法2 for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); arrmin[i] = arr[i]; arrmax[i] = arr[i]; } maopao1(arrmin);//调用冒泡后获得有序数组(小到大) maopao2(arrmax);//调用冒泡后获得有序数组(大到小) //【Arrays.equals(数组1,数组2)】 可直接用来比较两个数组 if (Arrays.equals(arr, arrmin)) { System.out.print("sorted"); } else { if (Arrays.equals(arr, arrmax)) { System.out.print("sorted"); } else { System.out.print("unsorted"); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); //要输入数据的数组 List<Integer> inputList = new ArrayList<>(); //与输入数组进行判断的数组 List<Integer> toJudgeSortList = new ArrayList<>(); //用来判断是否有序 //boolean toJudgeSort = false; //用来输入正常数据 for(int i = 0; i < n;i++){ inputList.add(scanner.nextInt()); } //将输入的正常数据复制一份进去,随后进行排序,最后判断是否有序 for(int j = 0;j < inputList.size();j++){ toJudgeSortList.add(inputList.get(j)); } Collections.sort(toJudgeSortList); //判断是否有序 //首先先正序排序后进行判断,正确则有序,否则进行逆序后再判断 if(inputList.equals(toJudgeSortList)){ System.out.println("sorted"); }else{ //逆序排序后进行判断,正确则有序,否则无序 Collections.reverse(toJudgeSortList); if(inputList.equals(toJudgeSortList)){ System.out.println("sorted"); }else{ System.out.println("unsorted"); } } /*for(int k = 0;k < inputList.size();k++){ //正序&&逆序 if(toJudgeSortList.get(k) != inputList.get(k) && toJudgeSortList.get(n - k - 1) != inputList.get(k)){ toJudgeSort = false; break; } } //输出 if(toJudgeSort){ System.out.println("unsorted"); }else{ System.out.println("sorted"); }*/ } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //两个正整数n,m int a[] = new int[n]; int b[] = new int[n]; int x = 0; for(int i = 0;i<n;i++){ a[i] = sc.nextInt(); } for(int i = 0;i<n;i++){ b[i] = a[i]; } Arrays.sort(b); for(int i = 0;i<n;i++){ if(a[i]!=b[i]){ x = x+1; } } if(x>0){ System.out.printf("unsorted"); } else{ System.out.printf("sorted"); } } } //只能判断正向有序
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input=new Scanner(System.in); int n=input.nextInt(); int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++){ a[i]=input.nextInt(); b[i]=a[i]; } Arrays.sort(a); int[] c=new int[n]; for(int i=0;i<a.length;i++){ //将排序的a反序,用来判断降序的数组 c[i]=a[a.length-1-i]; } if(Arrays.equals(a, b) || Arrays.equals(c,b)){ System.out.println("sorted"); } else System.out.println("unsorted"); } }
import java.io.*; public class Main{ public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int number = Integer.parseInt(bufferedReader.readLine()); String[] nums = bufferedReader.readLine().split(" "); int count = 0; int count2 = 0; int oneNum = Integer.parseInt(nums[0]); for (int i = 1; i < nums.length; i++) { int num = Integer.parseInt(nums[i]); if (oneNum <= num){ count++; }else if(oneNum >= num){ count2++; } oneNum=num; } if (count==number-1 || count2==number-1){ System.out.println("sorted"); }else{ System.out.println("unsorted"); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { list.add(sc.nextInt()); } List<Integer> newList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { newList.add(list.get(i)); } Collections.sort(list); boolean bool = true; for (int i = 0; i < list.size(); i++) { if (newList.get(i) != list.get(i) && newList.get(n - i - 1) != list.get(i)) { bool = false; break; } } if (bool) { System.out.println("sorted"); } else { System.out.println("unsorted"); } } }
import java.io.*; public class Main { public static void main(String args[])throws IOException{ BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); String s=bf.readLine(); int n=Integer.parseInt(s); String str[]=bf.readLine().split(" "); int flag1=0,flag2=1; for(int i=0;i<n-1;i++) { int x=Integer.parseInt(str[i]); int m=Integer.parseInt(str[i+1]); if(x>m) flag1=1;//都为降序,和flag2一样 else flag2=0;//都为升序,和flag1一样 } if(flag1==flag2) System.out.println("sorted"); else System.out.println("unsorted"); } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int pre = sc.nextInt(); int flag = 0; boolean judge = true; for (int i = 1; i < n; i++) { int cur = sc.nextInt(); if (cur - pre >= 0 && flag>=0){ flag = 1; }else if(cur - pre <= 0 && flag<=0){ flag = -1; }else{ judge = false; } pre = cur; } if (judge){ System.out.println("sorted"); }else { System.out.println("unsorted"); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean c = false; boolean d = false; int n = in.nextInt(); int [] a = new int[n]; for(int i=0;i<n;i++){ a[i] = in.nextInt(); } for(int i=0;i<n-1;i++){ if(a[i] == Math.min(a[i], a[i + 1])){ //判断升序 c = true; }else { c = false; break; } } for(int i=0;i<n-1;i++){ if (a[i] == Math.max(a[i], a[i + 1])){ //判断降序 d = true; }else{ d = false; break; } } if(c || d){ System.out.println("sorted"); }else{ System.out.println("unsorted"); } } }
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()) { int n=sc.nextInt(); int [] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int count=0; int count2=0; for(int i=0;i<n-1;i++) { int sub=arr[i+1]-arr[i]; if(sub>=0) { count++; } if(sub<=0) { count2++; } } if(count==(n-1)||count2==(n-1)) { System.out.println("sorted"); } else { System.out.println("unsorted"); } } } }