雙色球選號規(guī)則紅球是1~33選6個,藍球1~16選1個。
它有17721088種排列組合,
這個代碼實現(xiàn)了如何將一組雙色球號碼 轉(zhuǎn)換成第n個排列組合數(shù)字,
以及如何根據(jù)第n個排列組合數(shù)字生成一組雙色球號碼。
分析一下今年的中獎號碼所隱含的排列組合序號,也許你會找到規(guī)律,
哈哈,或許你能用它算出下一次的中獎號碼,趕快試試吧!
DoubleColorBall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import java.util.Arrays; public class DoubleColorBall { /** * 根據(jù)雙色球生成絕對序號(原理:排列組合算法) * a b c d e f 是紅球由小到大 g是藍球 */ 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; } /** * 根據(jù)絕對序號生成雙色球(原理:遍歷所有組合) * a b c d e f 是紅球由小到大 */ public static final String getBall( long ballIndex){ if (ballIndex> 17721088 )ballIndex=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++){ //最多循環(huán)1107568次,即為紅球組合數(shù) count++; if (redIndex==count){ return Arrays.toString( new int []{a,b,c,d,e,f, 1 +(( int )ballIndex- 1 )% 16 }); } } return null ; } /** * 計算組合數(shù)C(m,n) */ public static final int comp( int m, int n) { int sum= 1 ; for ( int i=m;i>m-n;i--)sum=sum*i; for ( int i=n;i> 1 ;i--)sum=sum/i; return sum; } public static void main(String[] args) { //11月29日開獎結果對應序號: System.out.println(getBallIndex( 6 , 20 , 28 , 29 , 30 , 31 , 12 )); //12964124 System.out.println(getBall( 12964124 )); //[6, 20, 28, 29, 30, 31, 12] //12月1日開獎結果對應序號: System.out.println(getBallIndex( 3 , 8 , 19 , 25 , 27 , 28 , 2 )); //7353378 System.out.println(getBall( 7353378 )); //[3, 8, 19, 25, 27, 28, 2] //12月3日開獎結果對應序號: System.out.println(getBallIndex( 13 , 17 , 19 , 20 , 22 , 25 , 11 )); //17009451 System.out.println(getBall( 17009451 )); //[13, 17, 19, 20, 22, 25, 11] System.out.println( "預測下次開獎號碼,趕快去買吧!" ); System.out.println(getBall(System.nanoTime())); } } |
另外附上java雙色球復式號碼,排列組合出所有單注號碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public class Test { /** * 雙色球復式組合 * @param redBall 紅球數(shù)組 * @param blueBall 籃球數(shù)組 * @return 產(chǎn)生的組合個數(shù) */ public static int getDoubleChromosphere(Integer [] redBall, int [] blueBall){ int count = 0 ; //產(chǎn)生的組合個數(shù) List<Integer> result = new LinkedList<Integer>();; //產(chǎn)生的雙色球組合 //外層循環(huán)控制籃球 for ( int i = 0 ;i < blueBall.length;i++){ //控制紅球 List<Integer> redList = new LinkedList<Integer>(); for (Integer j : redBall){ redList.add(j); } List<Integer> orign = new LinkedList<Integer>(); orign.addAll(redList); for ( int k = 0 ;k < redList.size();k++){ redList.remove(k); result = redList; //最后籃球的賦值 result.add(blueBall[i]); //輸出組合結果 System.out.print( "紅球為:\t" ); for ( int j = 0 ;j < result.size();j++){ if ( 6 == j){ System.out.println( "籃球為:\t" +result.get(j)); break ; } System.out.print(result.get(j)+ "\t" ); } System.out.println(); //清空redLisr,重新賦值 redList.clear(); redList.addAll(orign); //組合數(shù)加一 count++; } } return count; } } |