求一个java算法~~ 一个字符串如String s = "aaaassssdsdsdsdsfsfxxxzsdsdf";(a-z任意字符组成)写一个java程序求出出现次数最多的字母,以及出现的次数(如有相同次数的就全部求出)求解啊~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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. 我也是初学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); }} 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楼的类似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))); } } // 求出一个字符串中出现次数最多的字母,以及出现的次数 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); } eclipse的ide包是什么?开发java一定要用吗? 如何调用字体对话框的类???? 菜鸟问题(类,函数) 怎么将控制台输入的字符串,切开,并且每个字符见添加空格 求救了,斑竹,高手帮帮我了:) 如何用Javascript程序控制一个Applet的行为??? 为什么找不到bean. UDP传输总是相差80多个字节 下面的注解类是什么作用,小白不是很明白? 救教大神看下啊,不会运行... java堆栈溢出java.lang.OutOfMemoryError: Java heap space javascript vbscript 小问题
http://blog.csdn.net/justinavril/archive/2008/07/28/2724952.aspxThe algorithm in second topic can help you to realize your function.
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);
}
}
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);
}
具体的我就不说了,虽然不是很好,但是理解起来应该不是很难!
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)));
}
}
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);
}