java算法實現(xiàn)預(yù)測雙色球中獎號碼
雙色球選號規(guī)則紅球是1~33選6個,藍球1~16選1個。
它有17721088種排列組合,
這個代碼實現(xiàn)了如何將一組雙色球號碼 轉(zhuǎn)換成第n個排列組合數(shù)字,
以及如何根據(jù)第n個排列組合數(shù)字生成一組雙色球號碼。
分析一下今年的中獎號碼所隱含的排列組合序號,也許你會找到規(guī)律,
哈哈,或許你能用它算出下一次的中獎號碼,趕快試試吧!
DoubleColorBall.java
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日開獎結(jié)果對應(yīng)序號:
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日開獎結(jié)果對應(yīng)序號:
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日開獎結(jié)果對應(yīng)序號:
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("預(yù)測下次開獎號碼,趕快去買吧!");
System.out.println(getBall(System.nanoTime()));
}
}
另外附上java雙色球復(fù)式號碼,排列組合出所有單注號碼
public class Test {
/**
* 雙色球復(fù)式組合
* @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]);
//輸出組合結(jié)果
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;
}
}
- Java算法之數(shù)組冒泡排序代碼實例講解
- Java算法之串的簡單處理
- Java算法實現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解
- Java算法實現(xiàn)楊輝三角的講解
- Java算法之冒泡排序?qū)嵗a
- Java算法之最長公共子序列問題(LCS)實例分析
- java算法實現(xiàn)紅黑樹完整代碼示例
- Java算法之堆排序代碼示例
- java算法之二分查找法的實例詳解
- java算法導(dǎo)論之FloydWarshall算法實現(xiàn)代碼
- Java算法之遞歸算法計算階乘
- JAVA算法起步之插入排序?qū)嵗?/a>
- JAVA算法起步之堆排序?qū)嵗?/a>
- JAVA算法起步之快速排序?qū)嵗?/a>
- 關(guān)于各種排列組合java算法實現(xiàn)方法
- Java算法之時間復(fù)雜度和空間復(fù)雜度的概念和計算
相關(guān)文章
Java組件commons fileupload實現(xiàn)文件上傳功能
這篇文章主要為大家詳細介紹了Java組件commons fileupload實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Java開發(fā)編程到底是用idea好還是eclipse好
這篇文章主要介紹了Java開發(fā)編程到底是用idea好還是eclipse好,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
教你用Springboot實現(xiàn)攔截器獲取header內(nèi)容
項目中遇到一個需求,對接上游系統(tǒng)是涉及到需要增加請求頭,請求頭的信息是動態(tài)獲取的,需要動態(tài)從下游拿到之后轉(zhuǎn)給上游,文中非常詳細的介紹了該需求的實現(xiàn),需要的朋友可以參考下2021-05-05
Spring MVC Interceptor 實現(xiàn)性能監(jiān)控的功能代碼
本篇文章主要介紹了Spring MVC Interceptor 實現(xiàn)性能監(jiān)控的功能代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

