一、算法介绍 本博文主要写了彩票走势图中的遗漏值、出现总次数、平均遗漏值、最大遗漏值、最大连出值的计算逻辑。
二、图文简介 [这是XX网双色球的彩票走势图,博主这里是将彩票数据分区展示,以双色球为例,分了五个区域,本博文中的遗漏值是按照期数的降序排列计算的如下图]
三、工具类简介(请仔细看完工具类简介)
彩票走势图的工具类**LotteryTrend**中将彩票的名称彩票的id,及其对应的数字域数据域id,每个区域的起始数字,每个区域的长度等数据按照顺序一一对应定义。
另一个工具类**MissValue**就是彩票遗漏值的工具类,里面是具体的**彩票遗漏值、出现总次数、平均遗漏值、最大遗漏值、最大连出值**的计算方法,博主将开奖位置的遗漏值置为0,返给前端的。下面是这几个名词的定义:
**彩票遗漏值**:自上期开出到本期间隔的期数。
**出现总次数**:当前选定的彩票期数区间内该球的出现总次数。
**平均遗漏值**:平均遗漏值计算公式:总期数除以出现的总次数,如果总次数为0,该值为0;如果除不尽将计算结果向下取整。
**最大遗漏值**:历史开奖以来该球连续不出现的最大值,即为距离上次出现的间隔数的最大值。
**最大连出值**:**博主写的是选定期数内连续出现的最大值,与右图不符合**.
四、返回给前端的json数据
五、代码块
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/** * 彩票走势图工具类
* @author Administrator * */
public class LotteryTrend {
//所有彩票名称数组
public static final String[] titleArray = {"超级大乐透", "七星彩", "排列三", "排列五", "双色球", "福彩3D", "江西11选5", "11运夺金", "广东11选5", "上海11选5", "浙江11选5", "七乐彩", "重庆时时彩", "胜负彩", "安徽快三", "湖北快三", "吉林快三", "江苏快三", "江苏11选5", "浙江12选5"};
//彩票的id数组
public static final String[] lotteryidArray = {"19", "18", "2", "13", "16", "1", "9", "8", "10", "24", "27", "17", "3", "71", "32", "33", "34", "35", "28", "22"};
//每种彩票的区域或者数字域
public static final String[][] headArray = {{"红一区", "红二区", "红三区", "后区"}, {"第七位", "第六位", "第五位", "第四位", "第三位", "第二位", "第一位"}, {"百位", "十位", "个位"},
{"万位", "千位", "百位", "十位", "个位"}, {"红一区", "红二区", "红三区", "篮球"}, {"百位", "十位", "个位"},
{"万位", "千位", "百位", "十位", "个位"}, {"万位", "千位", "百位", "十位", "个位"}, {"万位", "千位", "百位", "十位", "个位"},
{"万位", "千位", "百位", "十位", "个位"}, {"万位", "千位", "百位", "十位", "个位"}, {"一区", "二区", "三区"},
{"万位", "千位", "百位", "十位", "个位"}, {"3", "1", "0"}, {"百位", "十位", "个位"},
{"百位", "十位", "个位"}, {"百位", "十位", "个位"}, {"百位", "十位", "个位"},
{"万位", "千位", "百位", "十位", "个位"}, {"万位", "千位", "百位", "十位", "个位"}};
//区域或者数字域的id
public static final String[][] idxArray = {{"area1", "area2", "area3", "back"}, {"num_info7", "num_info6", "num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"num_info3", "num_info2", "num_info1"},
{"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"area1", "area2", "area3", "num_info7"}, {"num_info3", "num_info2", "num_info1"},
{"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"num_info5",
"num_info4", "num_info3", "num_info2", "num_info1"}, {"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"},
{"area1", "area2", "area3"}, {"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}, {"3", "1", "0"}, {"num_info3", "num_info2", "num_info1"},
{"num_info3", "num_info2", "num_info1"}, {"num_info3", "num_info2", "num_info1"}, {"num_info3", "num_info2", "num_info1"}, {"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"},
{"num_info5", "num_info4", "num_info3", "num_info2", "num_info1"}};
//定义每个区域的起始数字
public static final String[][] beginNum = {{"01", "13", "25", "01"}, {"0", "0", "0", "0", "0", "0", "0"}, {"0", "0", "0"},
{"0", "0", "0", "0", "0"}, {"01", "12", "23", "01"}, {"0", "0", "0"},
{"1", "1", "1", "1", "1"}, {"1", "1", "1", "1", "1"}, {"1", "1", "1", "1", "1"}, {"1", "1", "1", "1", "1"},
{"1", "1", "1", "1", "1"}, {"01", "11", "21"}, {"0", "0", "0", "0", "0"}, {"1", "1", "1"}, {"1", "1", "1"},
{"1", "1", "1"}, {"1", "1", "1"}, {"1", "1", "1"}, {"1", "1", "1", "1", "1"}, {"1", "1", "1", "1", "1"}};
//定义每个区域的长度
public static final int[][] horizontalSpans = {{12, 12, 11, 12}, {10, 10, 10, 10, 10, 10, 10}, {10, 10, 10},
{10, 10, 10, 10, 10}, {11, 11, 11, 16}, {10, 10, 10},
{11, 11, 11, 11, 11}, {11, 11, 11, 11, 11}, {11, 11, 11, 11, 11}, {11, 11, 11, 11, 11},
{11, 11, 11, 11, 11}, {10, 10, 10}, {10, 10, 10}, {14, 14, 14}, {6, 6, 6},
{6, 6, 6}, {6, 6, 6}, {6, 6, 6}, {11, 11, 11, 11, 11}, {12, 12, 12, 12, 12}};
//是否需要两位,例如01或者1
public static final String[] isHaveTwoPosition = {"1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0"};
//冷热
public static final String[][] codeAndHotIdxArray = {{"num_info1", "num_info2", "num_info3", "num_info4", "num_info5", "num_info6", "num_info7"},
{"num_info1", "num_info2", "num_info3", "num_info4", "num_info5", "num_info6", "num_info7"}, {"num_info1", "num_info2", "num_info3"},
{"num_info3", "num_info2", "num_info1"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5", "num_info6", "num_info7"},
{"num_info3", "num_info2", "num_info1"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"},
{"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"},
{"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5", "num_info6", "num_info7"},
{"num_info3", "num_info2", "num_info1"}, {}, {"num_info1", "num_info2", "num_info3"}, {"num_info1", "num_info2", "num_info3"}, {"num_info1", "num_info2", "num_info3"},
{"num_info1", "num_info2", "num_info3"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"}, {"num_info1", "num_info2", "num_info3", "num_info4", "num_info5"}};
//封装走势图所有的数据
/**
* list 数据库查询到的当前彩种开奖数据集合
* issueCount 页面传来的指定的查看的总期数
* totalIssue 当前开奖数据历史以来的总期数
*/
public static Map trendInfo(List
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @admistrator 计算遗漏值的工具类
*/
public class MissingValue {
/**
* 获取整个区域的遗漏值
* lis 开奖数据的集合(从数据查询到的开奖数据集合)
* row 行数
* column 列数
*/
public static int[][] getMissingvalue(List> lis, Integer beginNum, Integer row, Integer horizontalSpan) {
int[][] arr = new int[row][horizontalSpan];
//获取整个区域的遗漏值
for (int k = 1; k <= horizontalSpan; k++) {
//获取每期开奖数据在当前列的位置
List countList = new ArrayList();
for (int i = 0; i <= lis.size() - 1; i++) {
for (int j = 0; j < lis.get(i).size(); j++) {
if (lis.get(i).get(j) > beginNum + horizontalSpan - 1) {
break;
}
if (lis.get(i).get(j) == k) {
countList.add(i);//记录当前列的开奖号码的行号
}
}
}
if (countList.size() != 0 && countList != null) {
int[] arrj = getOthers(countList, arr.length);
for (int j = 0; j < arrj.length; j++) {
arr[j][k - 1] = arrj[j];
}
} else {
for (int j = arr.length - 1; j >= 0; j--) {
arr[j][k - 1] = arr.length - j;
}
}
}
return arr;
}
//获取每列的遗漏值数据
public static int[] getOthers(List list, int row) {
int[] array = new int[row];
int n = 0;
while (true) {
if (list.size() == 1) {
if (list.get(n) == 0) {//中奖号码只有一个且在顶部
array[n] = 0;
for (int i = array.length - 1; i > 0; i--) {
array[i] = array.length - i;
}
} else if (list.get(n) == row - 1) {//中奖号码只有一个且在底部
for (int i = list.get(n); i >= 0; i--) {
array[i] = list.get(n) - i;
}
} else if (list.get(n) > 0 && list.get(n) < row - 1) {//中奖号码只有一个且在中间
for (int i = array.length - 1; i > list.get(n); i--) {
array[i] = array.length - i;
}
for (int i = list.get(n); i >= 0; i--) {
array[i] = list.get(n) - i;
}
}
} else if (list.size() > 1) {
if (n == 0) {//第1个开奖号码
if (list.get(n) == 0) {//当前中奖号码在顶部
array[n] = 0;
} else if (list.get(n) < row - 1) {//当前号码在中间
for (int i = list.get(n); i >= 0; i--) {
array[i] = list.get(n) - i;
}
}
} else if (n + 1 < list.size() && n > 0) {//当前为第n+1个开奖号码,而且不是最后一个,那么当前号码一定在中间
if (list.get(n) - list.get(n - 1) - 1 != 0) {//开奖号码有间隔
for (int i = list.get(n); i > list.get(n - 1); i--) {
array[i] = list.get(n) - i;
}
} else {//开奖号码没有间隔
array[list.get(n)] = 0;
}
} else if (n + 1 == list.size() && n > 0) {//当前开奖号码为这一列的最后一个开奖号码
if (list.get(n) - list.get(n - 1) - 1 > 0) {//开奖号码有间隔
if (list.get(n) == row - 1) {//当前中奖号码在底部
for (int i = list.get(n); i > list.get(n - 1); i--) {
array[i] = list.get(n) - i;
}
} else if (list.get(n) < row - 1) {//当前号码在中间
for (int i = list.get(n); i > list.get(n - 1); i--) {
array[i] = list.get(n) - i;
}
for (int i = array.length - 1; i >= list.get(n) + 1; i--) {
array[i] = array.length - i;
}
}
} else if (list.get(n) - list.get(n - 1) - 1 == 0) {//开奖号码没有间隔
if (list.get(n) == row - 1) {//当前中奖号码在底部
array[list.get(n)] = 0;
} else if (list.get(n) < row - 1) {//当前号码在中间
array[list.get(n)] = 0;
for (int i = array.length - 1; i > list.get(n); i--) {
array[i] = array.length - i;
}
}
}
break;
}
}
if (n == list.size() - 1) {
break;
}
n++;
}
return array;
}
/**
* 处理开奖数据的方法
* 实现的功能:将每一期在当前区域的开奖数据获取出来,并转换为int 类型
* lis 开奖数据的集合
*/
public static List> convertAwardData(List> lis) {
List> list = new ArrayList<>();
for (int i = 0; i < lis.size(); i++) {
List lii = new ArrayList<>();
String[] sp = lis.get(i).get("result").replaceFirst("\\+", ",").split(",");
for (int j = 0; j < sp.length; j++) {
lii.add(Integer.parseInt(sp[j]));
}
list.add(lii);
}
return list;
}
/**
* 计算每列开奖的总次数(即:出现总次数)
*
* arr 遗漏值数据的数组
*/
public static List getTotal(int[][] arr) {
List list = new ArrayList<>();
for (int i = 0; i < arr[0].length; i++) {
int count = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[j][i] == 0) {
count++;
}
}
list.add(count);
}
return list;
}
/**
* 计算平均遗漏值
*
* lis 出现总次数集合
* row 总行数
*/
public static List getCoverage(List lis, Integer row) {
List list = new ArrayList<>();
for (int i = 0; i < lis.size(); i++) {
if (lis.get(i) != 0) {
list.add(row / lis.get(i));
} else {
list.add(0);
}
}
return list;
}
/**
* 计算最大遗漏值
*
* arr 遗漏值的集合
*/
public static List getMax(int[][] arr) {
List ll = new ArrayList();
for (int i = 0; i < arr[0].length; i++) {
int max = arr[0][i];
for (int j = 0; j < arr.length; j++) {
if (arr[j][i] > max)
max = arr[j][i];
}
ll.add(max);
}
return ll;
}
/**
* 计算最大连出值
*
* arr 遗漏值的集合
*/
public static List getContinuous(int[][] arr) {
List list = new ArrayList<>();
for (int i = 0; i < arr[0].length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < arr.length; j++) {
sb.append(arr[j][i]);
}
list.add(max(sb.toString()));
}
return list;
}
//统计相同字符连续出现的最大子序列的长度
public static int max(String s) {
int max = 0, tmp_m = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
tmp_m++;
} else {
max = max > tmp_m ? max : tmp_m;
tmp_m = 1;
}
}
max = max > tmp_m ? max : tmp_m;//最后的连续数与最大连续的比较
return max;
}
}