java算法实现预测双色球中奖号码
作者:hebedich 时间:2022-06-19 17:01:22
双色球选号规则红球是1~33选6个,蓝球1~16选1个。
它有17721088种排列组合,
这个代码实现了如何将一组双色球号码 转换成第n个排列组合数字,
以及如何根据第n个排列组合数字生成一组双色球号码。
分析一下今年的中奖号码所隐含的排列组合序号,也许你会找到规律,
哈哈,或许你能用它算出下一次的中奖号码,赶快试试吧!
DoubleColorBall.java
import java.util.Arrays;
public class DoubleColorBall {
/**
* 根据双色球生成绝对序号(原理:排列组合算法)
* 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;
}
/**
* 根据绝对序号生成双色球(原理:遍历所有组合)
* 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++){//最多循环1107568次,即为红球组合数
count++;
if(redIndex==count){
return Arrays.toString(new int[]{a,b,c,d,e,f,1+((int)ballIndex-1)%16});
}
}
return null;
}
/**
* 计算组合数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双色球复式号码,排列组合出所有单注号码
public class Test {
/**
* 双色球复式组合
* @param redBall红球数组
* @param blueBall篮球数组
* @return产生的组合个数
*/
public static int getDoubleChromosphere(Integer [] redBall,int [] blueBall){
int count = 0;//产生的组合个数
List<Integer> result = new LinkedList<Integer>();;//产生的双色球组合
//外层循环控制篮球
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);
//组合数加一
count++;
}
}
return count;
}
}
标签:java,双色球,中奖号码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
idea 打包maven项目忽略test文件的操作
2021-10-16 07:21:14
![](https://img.aspxhome.com/file/2023/1/66631_0s.jpg)
在Spring中编写事务的介绍
2021-12-15 10:33:18
Mybatis @SelectKey用法解读
2022-04-18 18:01:35
SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案
2021-11-26 17:57:22
![](https://img.aspxhome.com/file/2023/7/88137_0s.png)
Java中用enum结合testng实现数据驱动的方法示例
2021-08-04 07:44:21
![](https://img.aspxhome.com/file/2023/9/65179_0s.png)
spring mvc中@RequestBody注解的作用说明
2022-04-07 14:15:02
![](https://img.aspxhome.com/file/2023/4/62764_0s.png)
springboot使用mybatis开启事务回滚
2022-09-10 05:15:55
![](https://img.aspxhome.com/file/2023/9/87369_0s.png)
Java和C#输入输出流的方法(详解)
2022-06-24 09:21:02
SpringBoot基于Sentinel在服务上实现接口限流
2023-11-27 17:19:25
![](https://img.aspxhome.com/file/2023/4/59574_0s.jpg)
详解Spring中的@PropertySource注解使用
2023-05-05 06:40:15
一篇文章带你从java字节码层理解i++和++i
2023-05-29 13:57:58
![](https://img.aspxhome.com/file/2023/9/63849_0s.png)
C#使用FileStream复制一个任意文件
2022-04-17 19:55:17
![](https://img.aspxhome.com/file/2023/3/86983_0s.png)
Java二叉搜索树基础原理与实现方法详解
2022-09-12 17:20:59
![](https://img.aspxhome.com/file/2023/4/65154_0s.png)
c# 实现控件(ocx)中的事件详解
2022-12-29 04:20:50
![](https://img.aspxhome.com/file/2023/5/88305_0s.jpg)
c# 调用Surfer软件,添加引用的具体操作方法
2023-12-06 17:05:37
![](https://img.aspxhome.com/file/2023/6/68656_0s.png)
Java多线程定时器Timer原理及实现
2022-03-03 09:53:51
浅谈Java转义符\\\\|
2022-12-11 16:25:43
Java中批处理框架spring batch详细介绍
2023-10-19 16:54:21
![](https://img.aspxhome.com/file/2023/5/83045_0s.png)
Mybatis注解实现多数据源读写分离详解
2021-12-15 21:44:16
![](https://img.aspxhome.com/file/2023/1/62211_0s.jpg)
eclipse maven 插件的安装和配置详解
2023-08-24 16:57:01
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)