- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.TreeSet;
- /*
- * 要求1:首先写一道程序,可以随机输出一注双色球的数。(这个我会写!)
- 要求2:让这道程序运行5000次,选出出现概率最大的7个数。
- (这个我真心不会了!)求高手为之解答一下~(尽量用循环和数组的方法来解决!)
- *
- */
- public class Double
- {
- public static void main(String[] args)
- {
- select();
- }
- public static void select()
- {
- Set<Integer> set = new TreeSet<Integer>();
- //String s = "";
- int key = 1, value = 0,blueKey = 1
- ,blueValue = 0,blueBall = 0,dafo = 0;
- TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
- TreeMap<Integer, Integer> tr = new TreeMap<Integer, Integer>();
- //TreeMap<Integer, Integer> td = new TreeMap<Integer, Integer>();
- while(key<34)
- {
- tm.put(key, value);
- key++;
- }
- while (blueKey<17)
- {
- tr.put(blueKey, blueValue);
- blueKey++;
- }
- Set<Integer> z = tm.keySet();
- Set<Integer> blueSet = tr.keySet();
- while (dafo<5000)
- {
- while (set.size() <= 5)
- {
- set.add((int) (Math.random() * 33 + 1));
- }
- for (Integer i : set)
- {
-
- for (Integer x: z)
- {
- if (i==x)
- {
- value = tm.get(x);
- value++;
- tm.put(x, value);
- }
- //System.out.println(x);
- }
- //s = s+i+",";
- }
- set.clear();
-
- //s = s.substring(0,s.length()-1);
- blueBall = (int) (Math.random() * 16 + 1);
-
- for (Integer blueKey1: blueSet)
- {
- if (blueBall==blueKey1)
- {
- blueValue = tr.get(blueKey1);
- blueValue++;
- tr.put(blueKey1, blueValue);
- }
- }
- dafo++;
- //s = "红球:"+s+"蓝球:"+blueBall;
- //System.out.println(s);
- }
- List<Map.Entry<Integer, Integer>> infoIds =
- new ArrayList<Map.Entry<Integer, Integer>>(tm.entrySet());
- Collections.sort(infoIds, new Comparator<Map.Entry<Integer, Integer>>() {
- public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
- return (o2.getValue() - o1.getValue());
- // return (o1.getKey()).toString().compareTo(o2.getKey());
- }
- });
- for (int i = 0; i < 6; i++) {
- String id = infoIds.get(i).toString();
- System.out.println("红球出现最多次数:"+id+"次");
- }
- List<Map.Entry<Integer, Integer>> infoIds1 =
- new ArrayList<Map.Entry<Integer, Integer>>(tr.entrySet());
- Collections.sort(infoIds1, new Comparator<Map.Entry<Integer, Integer>>() {
- public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
- return (o2.getValue() - o1.getValue());
- // return (o1.getKey()).toString().compareTo(o2.getKey());
- }
- });
- for (int i = 0; i < 1; i++) {
- String id = infoIds1.get(i).toString();
- System.out.print("蓝球出现最多次数:"+id+"次");
- }
- Set<Integer> u = tm.keySet();
- for (Integer x : u)
- {
- int t = tm.get(x);
- //System.out.println(x+"....."+t);
- }
- Set<Integer> r = tr.keySet();
- for (Integer x : r)
- {
- int t = tr.get(x);
- //System.out.println(x+"....."+t);
- }
- }
- }
复制代码 写这些代码学到很多东西!虽然不太符合题意!!但是我的结果比较满意!欢迎讨论!! |