闲着没事写的,献丑了。
重新编辑,排版一下。
package com;
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.Map.Entry;
public class DoubleColorBall {
// 這個是表示有幾組數
private static int SIZE = 100;
private static int COUNT_OF_REDBALL = 33;
private static int COUNT_OF_BLUEBALL = 16;
private static List allArray = new ArrayList();
private static List recommendNum = new ArrayList();
private static List analyse() {
Map<Integer, Integer> map33 = new TreeMap<Integer, Integer>();
Map<Integer, Integer> map16 = new TreeMap<Integer, Integer>();
for (int i = 1; i <= COUNT_OF_REDBALL; i++) {
map33.put(i, 0);
}
// System.out.println("COUNT_OF_REDBALL "+COUNT_OF_REDBALL+" map33.size: "+map33.size());
for (int i = 1; i <= COUNT_OF_BLUEBALL; i++) {
map16.put(i, 0);
}
// System.out.println("COUNT_OF_BLUEBALL "+COUNT_OF_BLUEBALL+" map16.size: "+map16.size());
for (int i = 0; i < SIZE; i++) {
List tempList = (ArrayList) allArray.get(i);
Integer tempNum33 = 0;
Integer tempCount33 = 0;
Integer tempNum16 = 0;
Integer tempCount16 = 0;
// 統計33號碼中出現的次數
for (int mark33 = 0; mark33 < 6; mark33++) {
tempNum33 = (Integer) tempList.get(mark33);
tempCount33 = (Integer) map33.get(tempNum33);
tempCount33++;
// System.out.println("tempCount33:"+tempCount33+" tempNum33:"+tempNum33);
map33.put(tempNum33, tempCount33);
}
// 統計16號碼中出現的次數
tempNum16 = (Integer) tempList.get(6);
tempCount16 = (Integer) map16.get(tempNum16);
// System.out.println("tempCount16:"+tempCount16+" tempNum16:"+tempNum16);
tempCount16++;
map16.put(tempNum16, tempCount16);
}
List<Entry> list_Data33 = getSortedHashtableByValue(map33);
List<Entry> list_Data16 = getSortedHashtableByValue(map16);
for (int i = 0; i < 6; i++) {
Entry e = list_Data33.get(i);
recommendNum.add(e.getKey());
}
Collections.sort(recommendNum);
recommendNum.add(list_Data16.get(0).getKey());
System.out.println("以下為各個數字出現的次數(格式 數字=次數):");
System.out.println(COUNT_OF_REDBALL + "個數字出現的次數:/n" + list_Data33);
System.out.println(COUNT_OF_BLUEBALL + "個數字出現的次數:/n" + list_Data16);
System.out.println("在" + SIZE + "組數據中出現次數最多的數字組成的新數據為:");
print(recommendNum);
return null;
}
private static void resetValue(int size, int red, int blue) { // System.out.println("resetValue:"+size+" "+red+" "+blue);
SIZE = size;
COUNT_OF_REDBALL = red;
COUNT_OF_BLUEBALL = blue;
}
public static void main(String[] args) {
// 可以输入三个参数,想要的组合数、红球数、蓝球数。
int size = 0;
int count_of_redball = 0;
int count_ofblueball = 0;
switch (args.length) {
case 3:
count_ofblueball = Integer
.parseInt(args[2].equals("0") ? COUNT_OF_BLUEBALL + ""
: args[2]);
size = Integer.parseInt(args[0].equals("0") ? SIZE + "" : args[0]);
count_of_redball = Integer
.parseInt(args[1].equals(0) ? COUNT_OF_REDBALL + ""
: args[1]);
break;
case 2:
count_of_redball = Integer
.parseInt(args[1].equals(0) ? COUNT_OF_REDBALL + ""
: args[1]);
size = Integer.parseInt(args[0].equals("0") ? SIZE + "" : args[0]);
count_ofblueball = COUNT_OF_BLUEBALL;
break;
case 1:
size = Integer.parseInt(args[0].equals("0") ? SIZE + "" : args[0]);
count_of_redball = COUNT_OF_REDBALL;
count_ofblueball = COUNT_OF_BLUEBALL;
break;
case 0:
size = SIZE;
count_of_redball = COUNT_OF_REDBALL;
count_ofblueball = COUNT_OF_BLUEBALL;
}
System.out.println("before-- resetValue:" + size + " "
+ count_of_redball + " " + count_ofblueball);
for (int i = 0; i < size; i++) {
resetValue(size, count_of_redball, count_ofblueball);
p();
}
resetValue(size, count_of_redball, count_ofblueball);
analyse();
// System.out.println(allArray.size());
}
public static List initList(int length) {
// System.out.println(length);
List array = new ArrayList();
for (int i = 1; i <= length; i++) {
array.add(new Integer(i));
}
return array;
}
public static void p() {
List<Integer> array33 = initList(COUNT_OF_REDBALL);
List<Integer> array16 = initList(COUNT_OF_BLUEBALL);
List objList = new ArrayList();
// get Red ball;
for (int i = 0; i < 6; i++) {
Integer temp = 0;
int index = 0;
index = new Integer((int) (Math.random() * COUNT_OF_REDBALL));
COUNT_OF_REDBALL = COUNT_OF_REDBALL - 1;
temp = array33.get(index);
// System.out.println("REDBALL "+COUNT_OF_REDBALL+" "+i+"index"+index+" temp"+temp);
array33.remove(index);
objList.add(temp);
}
Collections.sort(objList);
// get Blue ball;
// System.out.println("COUNT_OF_BLUEBALL:"+COUNT_OF_BLUEBALL);
Integer blueBall = array16
.get((int) (Math.random() * COUNT_OF_BLUEBALL));
objList.add(blueBall);
// 將所有的組數添加到allArray中做分析處理;
allArray.add(objList);
print(objList);
}
public static void print(List list) {
// System.out.println("daxiao:"+list.size());
if (list != null) {
for (int i = 0; i < list.size(); i++) {
Integer t = (Integer) list.get(i);
String s = "";
if (t < 10)
s = "0" + t.toString();
else
s = t.toString();
if (i != 5)
s += " ";
else
s += " + ";
System.out.print(s);
}
System.out.println();
} else {
System.out.println("list is null");
}
}
/** * 倒序排列 * 示例: key = num33 ,value = count33 * @param h * @return */
@SuppressWarnings("unchecked")
public static List getSortedHashtableByValue(Map h) {
Set set = h.entrySet();
Entry e = null;
List list_Data = new ArrayList(set);
Collections.sort(list_Data, new Comparator() {
public int compare(Object arg0, Object arg1) {
Long key1 = Long.valueOf(((Map.Entry) arg0).getValue()
.toString());
Long key2 = Long.valueOf(((Map.Entry) arg1).getValue()
.toString());
return key2.compareTo(key1);
}
});
return list_Data;
}
}