既然你要返回数量最多的那个数,为什么web方法的返回值为String?我改成int了,程序如下: public class Test { public int web(int a[]){ int i,j,temp,count,max; int b[]=new int[10]; for(i=0;i<a.length;i++){ temp=a[i]; count=1; for(j=i+1;j<a.length;j++){ if(a[j]==temp) count++; } b[i]=count; } max=0; for(i=1;i<b.length;i++){ if(b[max]<b[i]) max=i; } return max; }
public static void main(String args[]){ int a[]={0,1,2,3,3,3,4,4,2,5}; int result; Test t=new Test(); result=t.web(a); System.out.println(result); }
} 输出结果为3,就是说3出现的次数最多
写错了一点,应该是return a[max];整个程序如下: public class Test { public int web(int a[]){ int i,j,temp,count,max; int b[]=new int[10]; for(i=0;i <a.length;i++){ temp=a[i]; count=1; for(j=i+1;j <a.length;j++){ if(a[j]==temp) count++; } b[i]=count; } max=0; for(i=1;i <b.length;i++){ if(b[max] <b[i]) max=i; } return a[max]; }
public static void main(String args[]){ int a[]={0,1,2,3,3,3,4,4,2,5}; int result; Test t=new Test(); result=t.web(a); System.out.println(result); }
}
public class Text{public String web(int a[]){ int[]count=new int[10];//存储0-9数字出现的次数 for(int i=0;i<a.length;i++){ count[a[i]]++; } int index = 0; for(int i = 0;i < count.length;i++){ if(count[index] <= count[i]){ index = i; } } System.out.println(""+index); } public static void main(string[]args){ int a[]={0,1,2,3,3,3,4,4,2,5}; web(a); } }高手给完善下,谢谢
仔细考虑了一下,觉得还是用Map做较好,可以解决有两个元素的个数都一样都是最多的情况,用Map实现如下(用的遍历可能多了点):public class Test { /** * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
int[] a = new int[] { 0, 1, 2, 3, 3, 3, 4, 4, 4, 2, 5 };
Map<Integer, Integer> map = web(a);
System.out.println(map);
}
public static Map<Integer, Integer> web(int[] a) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) { if (map.containsKey(a[i])) { int count = map.get(a[i]).intValue(); map.put(a[i], ++count); } else { map.put(a[i], 1); } }
Set entrySets = map.entrySet();
int maxValue = 0;
Iterator it = entrySets.iterator(); if (it.hasNext()) { Entry en = (Entry)it.next(); maxValue = (Integer) en.getValue(); }
while (it.hasNext()) { Entry en = (Entry)it.next(); int newValue = (Integer) en.getValue(); if (newValue > maxValue) { maxValue = newValue; } }
Map<Integer, Integer> newMap = new HashMap<Integer, Integer>();
Set<Integer> keySet = (Set<Integer>)map.keySet(); for (Integer key : keySet) { if (map.get(key) == maxValue) { newMap.put(key, maxValue); } }
return newMap;
}}输出结果: {3=3, 4=3}
import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set;public class FindMax2 { public static void printMaxCount(int[] array) { HashMap hm = new HashMap(); int maxCount = 0; int tempCount = 0; for (int i = 0; i < array.length; i++) { tempCount = 1; if (hm.containsKey(array[i])) { tempCount = (Integer) hm.get(new Integer(array[i])); tempCount++; } if (tempCount > maxCount) { maxCount = tempCount; } hm.put(array[i], tempCount); } Set<Map.Entry> set = hm.entrySet(); for (Map.Entry entry : set) { if (entry.getValue().equals(maxCount)) { System.out.println(entry.getKey()); } } } public static void main(String[] args) throws IOException { int[] ttt = { 0, 1, 2, 3, 3, 3, 4, 4, 4, 2, 5 }; printMaxCount(ttt); }}
感觉public String web(int a[]){} 这个似乎和最大数又没啥关系.
public class Test { public int web(int a[]){
int i,j,temp,count,max;
int b[]=new int[10];
for(i=0;i<a.length;i++){
temp=a[i];
count=1;
for(j=i+1;j<a.length;j++){
if(a[j]==temp)
count++;
}
b[i]=count;
}
max=0;
for(i=1;i<b.length;i++){
if(b[max]<b[i])
max=i;
}
return max;
}
public static void main(String args[]){
int a[]={0,1,2,3,3,3,4,4,2,5};
int result;
Test t=new Test();
result=t.web(a);
System.out.println(result);
}
}
输出结果为3,就是说3出现的次数最多
public class Test { public int web(int a[]){
int i,j,temp,count,max;
int b[]=new int[10];
for(i=0;i <a.length;i++){
temp=a[i];
count=1;
for(j=i+1;j <a.length;j++){
if(a[j]==temp)
count++;
}
b[i]=count;
}
max=0;
for(i=1;i <b.length;i++){
if(b[max] <b[i])
max=i;
}
return a[max];
}
public static void main(String args[]){
int a[]={0,1,2,3,3,3,4,4,2,5};
int result;
Test t=new Test();
result=t.web(a);
System.out.println(result);
}
}
int[]count=new int[10];//存储0-9数字出现的次数
for(int i=0;i<a.length;i++){
count[a[i]]++;
}
int index = 0;
for(int i = 0;i < count.length;i++){
if(count[index] <= count[i]){
index = i;
}
}
System.out.println(""+index);
}
public static void main(string[]args){
int a[]={0,1,2,3,3,3,4,4,2,5};
web(a);
}
}高手给完善下,谢谢
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[] {
0, 1, 2, 3, 3, 3, 4, 4, 4, 2, 5
};
Map<Integer, Integer> map = web(a);
System.out.println(map);
}
public static Map<Integer, Integer> web(int[] a) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) {
if (map.containsKey(a[i])) {
int count = map.get(a[i]).intValue();
map.put(a[i], ++count);
} else {
map.put(a[i], 1);
}
}
Set entrySets = map.entrySet();
int maxValue = 0;
Iterator it = entrySets.iterator();
if (it.hasNext()) {
Entry en = (Entry)it.next();
maxValue = (Integer) en.getValue();
}
while (it.hasNext()) {
Entry en = (Entry)it.next();
int newValue = (Integer) en.getValue();
if (newValue > maxValue) {
maxValue = newValue;
}
}
Map<Integer, Integer> newMap = new HashMap<Integer, Integer>();
Set<Integer> keySet = (Set<Integer>)map.keySet();
for (Integer key : keySet) {
if (map.get(key) == maxValue) {
newMap.put(key, maxValue);
}
}
return newMap;
}}输出结果:
{3=3, 4=3}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class FindMax2 {
public static void printMaxCount(int[] array) {
HashMap hm = new HashMap();
int maxCount = 0;
int tempCount = 0; for (int i = 0; i < array.length; i++) {
tempCount = 1;
if (hm.containsKey(array[i])) {
tempCount = (Integer) hm.get(new Integer(array[i]));
tempCount++;
}
if (tempCount > maxCount) {
maxCount = tempCount;
}
hm.put(array[i], tempCount);
} Set<Map.Entry> set = hm.entrySet();
for (Map.Entry entry : set) {
if (entry.getValue().equals(maxCount)) {
System.out.println(entry.getKey());
}
}
} public static void main(String[] args) throws IOException {
int[] ttt = { 0, 1, 2, 3, 3, 3, 4, 4, 4, 2, 5 };
printMaxCount(ttt);
}}
如果没有限制,还是用9楼的Map实现吧,虽然开销很大。
int[]count=new int[10];//存储0-9数字出现的次数
for(int i=0;i<a.length;i++){
count[a[i]]++;
}
int index = 0;
for(int i = 0;i < count.length;i++){
if(count[index] <= count[i]){
index = i;
}
}
System.out.println(""+index);
return ""+index;
}
public static void main(String[] args){
int[] a = new int[] {
0, 1, 2, 3, 3, 3, 4, 4, 4, 2, 5
};
String s=new Text().web(a);
System.out.println(s);
}
}