一个字符串如String s = "aaaassssdsdsdsdsfsfxxxzsdsdf";(a-z任意字符组成)
写一个java程序求出出现次数最多的字母,以及出现的次数(如有相同次数的就全部求出)
求解啊~

解决方案 »

  1.   

    Please refer to:
    http://blog.csdn.net/justinavril/archive/2008/07/28/2724952.aspxThe algorithm in second topic can help you to realize your function.
      

  2.   

    我也是初学Java,对API的使用不熟,还是习惯不过来C++的一些特性,所以,写出这种没风格的Code,希望各位帮我指正。
    import java.util.Scanner;public class Tmp {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] times = new int[26];
    String st = sc.nextLine();
    for(int i = 0; i < st.length(); i++) {
    times[st.charAt(i)-'a']++;
    }//计算各个字符出现的次数 int max = times[0];
    for(int i = 1; i < 26; i++) {
    if(times[i] > max) {
    max = times[i];
    }
    }//求字符出现最多的次数 String str = "abcdefghijklmnopqrstuvwxyz";
    for(int i = 0; i < 26; i++) {
    if(times[i] == max) {
    System.out.print(str.charAt(i) + " ");
    }
    }
    System.out.println("\n出现的次数为:" + max);
    }
    }
      

  3.   

       
            public void testSort(){
    String str = "jasdfdjaslkfj;aifjowejfonkasoe";
    HashMap<Character, Integer> res = new HashMap<Character, Integer>(); 
    char[] chars = str.toCharArray();
    for(int i = 0; i < chars.length; i++){
    char ch = chars[i];
    if(!res.containsKey(ch)){
    res.put(ch, 1);
    }else{
    res.put(ch, res.get(ch)+1);
    }
    }
    System.out.println(res);
    }
       具体的我就不说了,虽然不是很好,但是理解起来应该不是很难!
      

  4.   

    其实想找算法效率比较高的,这个朋友写的,跟4楼的类似
    String s = "aaaassssdsdsdsdsfsfxxxzsdsaaaaaaadf";
    System.out.println(s);
    int max=0;
    char c;
    int count;
    HashMap allWord = new HashMap();
    ArrayList maxWord = new ArrayList();
    for(int i=0; i<s.length(); i++) {
    c = s.charAt(i);
    if(allWord.containsKey(c)) {
    count = (Integer)allWord.get(c);
    allWord.put(c, ++count);
    if(count>max) {
    maxWord.clear();
    maxWord.add(c);
    max=count;
    }
    else if(count==max) {
    maxWord.add(c);
    }
    }
    else {
    count = 1;
    allWord.put(c, count);
    if(count>max) {
    maxWord.clear();
    maxWord.add(c);
    max=count;
    }
    else if(count==max) {
    maxWord.add(c);
    }
    }
    }
    for(int i=0; i<maxWord.size(); i++) {
    System.out.println("\nmax word:\n" + maxWord.get(i) + "/" + allWord.get(maxWord.get(i)));

    }
      

  5.   

    // 求出一个字符串中出现次数最多的字母,以及出现的次数
    public static void testNum(String s) {
    TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
    char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) {
    if (!map.containsKey(chars[i])) {
    map.put(chars[i], 1);
    } else {
    map.put(chars[i], map.get(chars[i]) + 1);
    }
    } System.out.println(map); Set<Map.Entry<Character, Integer>> set = map.entrySet();
    Iterator<Map.Entry<Character, Integer>> it = set.iterator();
    List<Integer> list = new ArrayList<Integer>();
    while (it.hasNext()) {
    list.add(it.next().getValue());
    }
    int max = Collections.max(list);
    it = set.iterator();
    Character key = null;
    while (it.hasNext()) {
    Map.Entry<Character, Integer> mss = it.next();
    int temp = mss.getValue();
    if (temp == max) {
    key = mss.getKey();
    }
    }
    System.out.println(key + "=" + max);
    }