利用組合數遞推公式,將紅球組合映射為唯一序號(0-based),再乘 16 加藍球值得到完整索引。
public static final int getBallIndex(int a,int b,int c,int d,int e,int f,int g) {
return (comp(33,6) - comp(34-a,6)
+ comp(33-a,5) - comp(34-b,5)
+ comp(33-b,4) - comp(34-c,4)
+ comp(33-c,3) - comp(34-d,3)
+ comp(33-d,2) - comp(34-e,2)
+ comp(33-e,1) - comp(33-f,1)) * 16 + g;
}
public static final String getBall(long ballIndex) {
if (ballIndex > 17721088) ballIndex %= 17721088;
int redIndex = (int)(ballIndex / 16);
int count = 0;
for (int a=1; a<29; a++)
for (int b=a+1; b<30; b++)
for (int c=b+1; c<31; c++)
for (int d=c+1; d<32; d++)
for (int e=d+1; e<33; e++)
for (int f=e+1; f<34; f++) {
count++;
if (redIndex == count) {
int blue = 1 + (int)((ballIndex-1) % 16);
return Arrays.toString(new int[]{a,b,c,d,e,f,blue});
}
}
return null;
}
public static final int comp(int m, int n) {
int sum = 1;
for (int i=m; i>m-n; i--) sum *= i;
for (int i=n; i>1; i--) sum /= i;
return sum;
}
// 2023-11-29 開獎:6,20,28,29,30,31 + 12
System.out.println(getBallIndex(6,20,28,29,30,31,12)); // 12964124
System.out.println(getBall(12964124)); // [6, 20, 28, 29, 30, 31, 12]
// 2023-12-01 開獎:3,8,19,25,27,28 + 2
System.out.println(getBallIndex(3,8,19,25,27,28,2)); // 7353378
System.out.println(getBall(7353378)); // [3, 8, 19, 25, 27, 28, 2]
public static int getDoubleChromosphere(Integer[] redBall, int[] blueBall) {
List result = new LinkedList<>();
int count = 0;
for (int b : blueBall) {
List reds = new LinkedList<>(Arrays.asList(redBall));
for (int i=0; i combo = new LinkedList<>(reds);
combo.remove(i); // 從 n 紅球中選 n-1?實際邏輯需根據玩法調整
combo.add(b);
System.out.println("紅球: " + combo.subList(0,6) + " 藍球: " + b);
count++;
}
}
return count;
}
基於組合數學的雙向映射,可用於歷史數據分析、走勢圖繪制、隨機生成注單等彩票輔助工具開發。