题目描述
给定n个字符串,请对n个字符串按照字典序排列。
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
示例1
输入:
9
cap
to
cat
card
two
too
up
boat
boot
输出:
boat
boot
cap
card
cat
to
too
two
up
2020.04:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String numstr = sc.nextLine();
int num = Integer.valueOf(numstr);
ArrayList<String> strs =new ArrayList();
while(num!=0){
String str = sc.nextLine();
strs.add(str);
num--;
}
TreeMap<String,String> ssmap = new TreeMap<>();
for (int i = 0; i < strs.size(); i++) {
if (ssmap.containsKey(strs.get(i))){
ssmap.put(strs.get(i) + i, strs.get(i));
}else {
ssmap.put(strs.get(i), strs.get(i));
}
}
Collection<String> values = ssmap.values();
for (String value : values) {
System.out.println(value);
}
}
}
2020.11
package thinking_in_java;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeMap<String, Integer> strings = new TreeMap<>();
int num = sc.nextInt();
while (num > 0) {
String s = sc.next();
if (strings.containsKey(s)) {
strings.put(s, strings.get(s) + 1);
} else {
strings.put(s, 1);
}
num--;
}
for (String s : strings.keySet()) {
Integer count = strings.get(s);
while (count>0){
System.out.println(s);
count--;
}
}
}
}
给定1到1000个字符串,要求按照字典序进行排序。输入包含字符串数量及各字符串,输出排序后的结果。示例展示了一组输入和其对应的正确输出。
2641

被折叠的 条评论
为什么被折叠?



