给定 n 个字符串,请对 n 个字符串按照字典序排列。
数据范围: ,字符串长度满足
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
数据输出n行,输出结果为按照字典序排列的字符串。
9 cap to cat card two too up boat boot
boat boot cap card cat to too two up
import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = Integer.valueOf(in.nextLine()); ArrayList<String> arr = new ArrayList<>(); for(int i=0;i<num;i++) { arr.add(in.nextLine()); } Collections.sort(arr); for(String str:arr) { System.out.println(str); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<String> r = new ArrayList(); while (n -- > 0) { r.add(in.next()); } Collections.sort(r); r.forEach(System.out::println); } }
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.hasNext()) { // 注意 while 处理多个 case int a = in.nextInt(); String [] str = new String[a]; for (int i = 0; i < a; i++) { str[i] = in.next(); } // 使用选择排序 for (int i = 0; i < a; i++) { int minIndex = i; for (int j = i + 1; j < a; j++) { if (!isSwamp(str[minIndex], str[j])) { minIndex = j; }; } if (minIndex != i) { String temp = str[i]; str[i] = str[minIndex]; str[minIndex] = temp; } } for (String str1 : str) { System.out.println(str1); } } } public static boolean isSwamp(String word1, String word2) { int strLength = Math.min(word1.length(), word2.length()); for (int i = 0; i < strLength; i++) { // 字符一比较出大小,就结束循环 if (word1.charAt(i) < word2.charAt(i)) { return true; } if (word2.charAt(i) < word1.charAt(i)){ return false; } } // 遍历部分字符相同,则比较长度,长度较大的必定排在后面 return word1.length() > word2.length()? false:true; } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = in.next(); } Arrays.sort(arr); for (String s : arr) { System.out.println(s); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); String[] alls = new String[n]; for (int i = 0; i < n; i++) { alls[i] = in.next(); } quickSort(alls, 0, n - 1); for (String all : alls) { System.out.println(all); } } } private static void quickSort(String[] alls, int l, int r) { if (l >= r) return; int mid = l + r >> 1, i = l - 1, j = r + 1; String x = alls[mid]; while (i < j) { while (compare(x, alls[++i]) > 0) ; while (compare(x, alls[--j]) < 0) ; if (i < j) { String tmp = alls[i]; alls[i] = alls[j]; alls[j] = tmp; } } quickSort(alls, l, j); quickSort(alls, j + 1, r); } public static int compare(String s1, String s2) { int l1 = s1.length(); int l2 = s2.length(); for (int i = 0; i < l1 && i < l2; i++) { if (s1.charAt(i) > s2.charAt(i)) return 1; if (s1.charAt(i) < s2.charAt(i)) return -1; } return Integer.compare(l1, l2); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); String[] words = new String[size]; for(int i = 0; i < words.length; i++) { words[i] = in.next(); } Arrays.sort(words); for (String word : words) { System.out.println(word); } } }
import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet; import java.util.Map; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int total = Integer.parseInt(in.nextLine()); TreeMap<String, Integer> tm = new TreeMap<>(); for (int i = 0; i < total; i++) { String key = in.nextLine(); if (tm.containsKey(key)) { tm.put(key, tm.get(key) + 1); } else { tm.put(key, 1); } } for (Map.Entry<String, Integer> entry : tm.entrySet()) { for (Integer integer = 0; integer < entry.getValue(); integer++) { System.out.println(entry.getKey()); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int num = in.nextInt(); List<String> list = new ArrayList<>(); while(in.hasNext()){ list.add(in.next()); } Collections.sort(list); for(String s : list){ System.out.println(s); } } }
一开始用的是TreeSet,发现会丢失重复数据,后来换成了TreeMap。
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, Integer> tm = new TreeMap<String, Integer>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); for (int i = 0; i < a; i++) { String s = in.next(); if (tm.containsKey(s)) { tm.put(s, tm.get(s) + 1); } else { tm.put(s, 1); } } } tm.forEach((key, value)-> { for (int i = 0; i < value; i++) { System.out.println(key); } }); } }
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String first = in.next(); ArrayList<String> list = new ArrayList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case list.add(in.next()); } // 创建一个String类型的比较器. Comparator<String> com=Comparator.naturalOrder(); // 通过比较器对list中的数据进行排序 list.sort(com); for (String str : list) { System.out.println(str); } } }
直接使用Collections.sort(list)排序就行了,默认是升序
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { int length = input.nextInt(); int i = 0; List<String> list = new ArrayList<>(); while (i < length) { list.add(input.next()); i++; } Collections.sort(list); list.forEach(System.out::println); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; public class Main { public static void main(String[] args) throws IOException { useArraysSort(); } //方法一使用数组排序 static void useArraysSort(){ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String [] str = new String[Integer.parseInt( reader.readLine())]; String n = ""; for (int i = 0; (n = reader.readLine()) != null; i++) { str[i] = n; } Arrays.sort(str); for (String s : str) { System.out.println(s); } } //方法二使用优先队列 static void usePriorityQueue(){ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PriorityQueue<String> priorityQueue = new PriorityQueue<>(Integer.parseInt( reader.readLine())); ; String n = ""; while ((n = reader.readLine()) != null) { priorityQueue.offer(n); } while (!priorityQueue.isEmpty()) { System.out.println(priorityQueue.poll()); } } }