public class 质数 { public static void main(String[] args) { for(int i = 0;i < 100;i++){ if(zhishu(i)) System.out.print(i); } } public static boolean zhishu(int n){ boolean flag = true; if (n==0&&n==1) { flag = false; }else{ for (int i = 2; i <= Math.sqrt(n); i++) { if (n%i==0) { flag = false; } } } return flag;}//求1-100的质数 Scanner sc = new Scanner(System.in); String s = sc.nextLine(); HashMap<Character, Integer> hm = new HashMap<>(); char[] array = s.toCharArray(); for (Character c : array) { if (!hm.containsKey(c)) { hm.put(c, 1); }else { hm.put(c, hm.get(c) + 1); } } Collection<Integer> values = hm.values(); TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() { @Override public int compare(Integer i1, Integer i2) { int num = i2 - i1; return num == 0 ? 1 : num; } });//键盘录入一段字符,并且统计个数,按次数排序 ts.addAll(values); for (Integer integer : ts) { for (Character c : hm.keySet()) { if (hm.get(c) == integer) { System.out.print(c + "(" + hm.get(c) + ")"); } } }
|