Java 冒泡排序
字符统计
http://www.nowcoder.com/questionTerminal/c1f9561de1e240099bdb904765da9ad0
package project1; import java.io.*; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str = br.readLine()) != null) { ArrayList<Character> list_all = new ArrayList<Character>(); for(int i = 0; i < str.length(); i++) { list_all.add(str.charAt(i)); } ArrayList<Character> list_uni = new ArrayList<Character>(); ArrayList<Integer> list_occ = new ArrayList<Integer>(); for(int i = 0; i < str.length(); i++) { if(!list_uni.contains(list_all.get(i))) { list_uni.add(list_all.get(i)); list_occ.add(0); } } for(int i = 0; i < list_uni.size(); i++) { for(int j = 0; j < list_all.size(); j++) { if(list_uni.get(i).equals(list_all.get(j))) { list_occ.set(i, list_occ.get(i) + 1); } } } bubble(list_uni.size(), list_uni, list_occ); for(int i = 0; i < list_uni.size(); i++) { System.out.print(list_uni.get(i)); } System.out.println(""); } } public static void bubble(int n, ArrayList<Character> list_uni, ArrayList<Integer> list_occ) { if(n == 1) { return; } for(int i = 0; i < n - 1; i++) { if(list_occ.get(i) < list_occ.get(i + 1)) { int a = list_occ.get(i + 1); char b = list_uni.get(i + 1); list_occ.set(i + 1, list_occ.get(i)); list_occ.set(i, a); list_uni.set(i + 1, list_uni.get(i)); list_uni.set(i, b); } else if(list_occ.get(i) == list_occ.get(i + 1)) { if((int) list_uni.get(i) > (int) list_uni.get(i + 1)) { int a = list_occ.get(i + 1); char b = list_uni.get(i + 1); list_occ.set(i + 1, list_occ.get(i)); list_occ.set(i, a); list_uni.set(i + 1, list_uni.get(i)); list_uni.set(i, b); } } } bubble(n - 1, list_uni, list_occ); } }