首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java ITAT往届考试题

2012-10-21 
java ITAT往届试题求解答:编写一个Java应用程序,对用户输入的任意一组字符如{1,3,4,7,2,1,1,5,2},输出其中

java ITAT往届试题
求解答:编写一个Java应用程序,对用户输入的任意一组字符如{1,3,4,7,2,1,1,5,2},输出其中出现次数最多且数值最大的字符,并显示其出现次数

[解决办法]
package com.xyj.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

public class Test5 {
public static void main(String[] args) {
BufferedReader reader = null;

try {
reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();

StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c <= '9' && c >= '0') {
sb.append(c).append(",");
}
}
if (sb != null && sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}

String[] str = sb.toString().split(",");
int[] in = new int[str.length];
for (int i = 0; i < str.length; i++) {
in[i] = Integer.parseInt(str[i]);
}

Set<Integer> hash = new HashSet<Integer>();
for (int i : in) {
hash.add(i);
}

int num = hash.size();
int[] times = new int[num];
List<Integer> list = new ArrayList<Integer>(hash);
for (int i : in) {
times[list.indexOf(i)]++;
}

Map<Integer, Integer> map = new TreeMap<Integer, Integer>(/*
new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
};
}*/);
for (int i = 0; i < num; i++) {
map.put(list.get(i), times[i]);
}
List<Map.Entry<Integer, Integer>> mapList = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
Collections.sort(mapList, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1,
Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});

Set<Entry<Integer, Integer>> set = map.entrySet();
for (Iterator<Entry<Integer, Integer>> iter = set.iterator(); iter
.hasNext();) {
Map.Entry<Integer, Integer> entry = (Entry<Integer, Integer>) iter
.next();
System.out.println("数值:"+entry.getKey() + " 出现的次数为:" + entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

[解决办法]

Java code
import java.util.HashMap;import java.util.Map;public class Test {    public static void main(String[] args){        int[] array = new int[]{1,3,4,7,2,1,1,5,2};        Map<Integer, Integer> map = new HashMap<Integer, Integer>();        for (int i=0; i<array.length; i++){            if (map.get(array[i]) == null){                map.put(array[i], 1);            } else {                map.put(array[i], map.get(array[i]) + 1);            }        }        int times = 0;        int max = 0;        for (Map.Entry<Integer, Integer> entry: map.entrySet()){            if (entry.getValue() > times){                max = entry.getKey();                times = entry.getValue();            } else if (entry.getValue() == times && entry.getKey() > max){                max = entry.getKey();            }        }        System.out.println("max times num: " + max + " times:" + times);    }} 


[解决办法]

探讨
package com.xyj.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Co……

[解决办法]

Java code
import java.util.ArrayList;import java.util.Scanner;public class count {    public static void main(String[] args) {        ArrayList<g> array = new ArrayList<g>();        ArrayList<g> cont = new ArrayList<g>();        Scanner scan = new Scanner(System.in);        String str = scan.nextLine();        String[] ss = str.split("\\,");        for (String s : ss) {            g g1 = new g();            g1.setKey(Integer.parseInt(s));            array.add(g1);        }        int v = 0;        for (int i = 0; i < array.size(); i++) {            if (cont.contains(array.get(i))) {                int x = cont.indexOf(new g(array.get(i).getKey()));                v = cont.get(x).getValue();                cont.get(x).setValue(++v);            } else {                cont.add(array.get(i));            }        }        g g2 = new g();        for (int i = 0; i < cont.size(); i++) {            if (cont.get(i).getValue() > g2.getValue()) {                g2.setKey(cont.get(i).getKey());                g2.setValue(cont.get(i).getValue());            }            if (cont.get(i).getValue() == g2.getValue()) {                if (cont.get(i).getKey() > g2.getKey()) {                    g2.setKey(cont.get(i).getKey());                }            }        }        System.out.println(g2.getKey() + "," + g2.getValue());    }}class g {    private int key;    private int value;    public g() {        key = 0;        value = 1;    }    public g(int key) {        this.key = key;    }    public int getKey() {        return key;    }    public void setKey(int key) {        this.key = key;    }    public int getValue() {        return value;    }    public void setValue(int value) {        this.value = value;    }    @Override    public boolean equals(Object obj) {        g mg = (g) obj;        if (mg.getKey() == this.getKey())            return true;        return false;    }} 

热点排行