题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine().toLowerCase();
char[] chars = s.toCharArray();
TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(1, 0);
map.put(2, 0);
map.put(3, 0);
map.put(4, 0);
for (char c : chars) {
if (c >= '0' && c <= '9') {
map.put(3, map.getOrDefault(3, 0) + 1);
} else if (c >= 'a' && c <= 'z') {
map.put(1, map.getOrDefault(1, 0) + 1);
} else if (c == ' ') {
map.put(2, map.getOrDefault(2, 0) + 1);
} else {
map.put(4, map.getOrDefault(4, 0) + 1);
}
}
for (Integer n : map.keySet()) {
System.out.println(map.get(n));
}
}
}
主要是看熟不熟ASCII码吧
查看8道真题和解析
三奇智元机器人科技有限公司公司福利 82人发布