【名词解释】
第一行输入一个整数
代表给定的单词个数。
此后
行,每行输入一个长度
,由大小写字母构成的字符串
,代表一个单词。
一共
行,每行输出一个字符串,代表排序后的结果。第一行输出字典序最小的单词。
11 cap to cat card two too up boat boot AA Aa
AA Aa boat boot cap card cat to too two up
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = in.nextInt(); List list = new ArrayList<String>(); for(int i=0 ; i<count; i++){ String str =in.next(); list.add(str); } Collections.sort(list); list.forEach(System.out::println); } }
import java.util.Scanner; import java.util.PriorityQueue; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PriorityQueue<String> queue=new PriorityQueue<>(); int len=in.nextInt(); for(int i=0;i<len;i++){ String str=in.next(); queue.add(str); } while(!queue.isEmpty()){ System.out.println(queue.poll()); } in.close(); } }
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(); // 读取字符串的数量 in.nextLine(); // 读取换行符 String[] strings = new String[n]; // 创建字符串数组 for (int i = 0; i < n; i++) { strings[i] = in.nextLine(); // 读取每个字符串 } Arrays.sort(strings); // 使用Arrays类的sort方法对字符串进行排序 for (String str : strings) { System.out.println(str); // 输出排序后的字符串 } in.close(); // 关闭扫描器 } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), i = 0, j; String[] dict = new String[n]; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { dict[i++] = in.next(); } // 依照自己编写的比较逻辑进行排序 for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (compare(dict[i], dict[j]) > 0) { String t = dict[i]; dict[i] = dict[j]; dict[j] = t; } } } for (i = 0; i < n; i++) { System.out.println(dict[i]); } } public static int compare(String a, String b) { /* 字符串字典比较,返回1,-1或0 */ int len = a.length() > b.length()? b.length(): a.length(); /* 终止条件:有一个子串被消耗完 */ if (len == 0) { if (a.length() == b.length()) { return 0; } else if (a.length() > 0) { return 1; } else { return -1; } } if (a.charAt(0) == b.charAt(0)) { /* 按照递归思想,当前一位相同时比较剩余的子串 */ return compare(a.substring(1), b.substring(1)); } else if (a.charAt(0) > b.charAt(0)) { return 1; } else { return -1; } } }
import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.Stream; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.out.println("请输入要比较的单词数目:"); int n = sc.nextInt(); String[] strings = new String[n]; for (int i = 0; i < n; i++) { //System.out.println("请输入要比较的单词:"); strings[i] = sc.next(); } Arrays.sort(strings); for (String string : strings) { System.out.println(string); } sc.close(); } }方法二:
// Scanner sc = new Scanner(System.in); // List<String> wordList = new ArrayList<>(); // System.out.println("请输入要排序的字符串的个数:"); // int n = sc.nextInt(); // for (int i = 0; i < n; i++) { // System.out.println("请输入单词:"); // String word = sc.next(); // wordList.add(word); // } // wordList.sort(String::compareTo); // wordList.forEach(System.out::println);
import java.util.Scanner; import java.util.Arrays; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = Integer.parseInt(in.nextLine()); String[] Set = new String[size]; for (int times = 0; times < size; times++) { Set[times]=in.nextLine(); } Arrays.sort(Set); // 注意 hasNext 和 hasNextLine 的区别 for (String tmp : Set) { System.out.println(tmp); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = in.nextInt(); String[] str =new String[n]; for(int i=0;i<n;i++){ str[i]=in.next(); } for(int i=0;i<n;i++){ for(int j=0;j<n-i-1;j++){ if(str[j].compareTo(str[j+1])>0){ String temp; temp=str[j]; str[j]=str[j+1]; str[j+1]=temp; } } } for(String word: str){ System.out.println(word); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); String[] arr = new String[rows]; for (int i = 0; i < rows; i++) { arr[i] = scanner.next(); } Arrays.sort(arr); for (String word : arr) { System.out.println(word); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String line; int cnt = 0; int linecnt = 1; String[] array= null;; // 保存数据 while (in.hasNextLine()) { // 注意 while 处理多个 case if(linecnt == 1){ cnt = in.nextInt(); in.nextLine(); array = new String[cnt]; }else{ array[linecnt - 2] = in.nextLine(); } if(linecnt == cnt + 1){ break; } linecnt ++; } // 排序 String temp; for(int i = 0; i<array.length; i ++){ for(int j = 0; j < array.length - i - 1;j++){ if(array[j].compareTo(array[j + 1]) > 0){ temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } //输出(或在排序时找出最小的依次输入,减少一次for过程) for(String e :array){ System.out.println(e); } } }
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; } }