import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class MainControl {
private static final int NEED_ELE = 5;
private static final int MAX_NUM = 11;
public static void main(String[] args) {
getElement();
for (int v = 15; v <= 45; v++) {
System.out.print(v + ":");
Set<Entry<List<Integer>, Integer>> entrySet = dataMap.entrySet();
int count = 0;
List<List<Integer>> data = new ArrayList<List<Integer>>();
for (Entry<List<Integer>, Integer> entry : entrySet) {
Integer value = entry.getValue();
if (value == v) {
count++;
data.add(entry.getKey());
}
}
System.out.print("共" + count + "注,");
System.out.print(" 附注:");
for (List<Integer> dataLs : data) {
System.out.print(dataLs);
}
System.out.println(" 完!");
}
}
private static void getElement() {
int totalSize = combination(5);
int index = 1;
while(index <= totalSize) {
if (getPerEle()) {
index++;
}
}
}
private static Map<List<Integer>, Integer> dataMap = new HashMap<List<Integer>, Integer>();
private static boolean getPerEle() {
List<Integer> dataLs = new ArrayList<Integer>();
for (int i = 1; dataLs.size() < NEED_ELE; i++) {
int ran = random(MAX_NUM);
if (!dataLs.contains(ran)) {
dataLs.add(ran);
}
}
Collections.sort(dataLs);
if (dataMap.isEmpty() || notContain(dataLs)) {
dataMap.put(dataLs, sum(dataLs));
return true;
}
return false;
}
private static boolean notContain(List<Integer> dataLs) {
for (List<Integer> dataTmp : dataMap.keySet()) {
if (dataTmp.toString().equals(dataLs.toString())) {
return false;
}
}
return true;
}
private static int sum(List<Integer> dataLs) {
int sum = 0;
for (Integer perData : dataLs) {
sum += perData;
}
return sum;
}
private static int random(int n) {
return (int)Math.floor((Math.random() * n)) + 1;
}
private static int combination(int m) {
return factorial(MAX_NUM)/factorial(m)/factorial(MAX_NUM-m);
}
private static int factorial(int n) {
if (n == 1)
return 1;
return n * factorial(n-1);
}
}
11选5数据
最新推荐文章于 2021-10-11 09:54:08 发布