CP机选算法

java 复制代码
package com.cp;


import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * desc
 */
public class CpRandomUtil {
    private static final Logger logger = LoggerFactory.getLogger(CpRandomUtil.class);
    public static void main(String[] args) throws IOException {
        printSsq();
//        tjRate();
    }

    private static void printSsq() throws IOException {
        String ssq = IOUtils.toString(new FileInputStream("D:\\我的\\ssq.txt"), Charset.forName("UTF-8"));
        String[] strs = ssq.split("\n");
        List<List<String>> allSsq = new ArrayList<>();
        for (String str : strs) {
            String[] split = str.split(",");
            List<String> ss = new ArrayList<>();
            for (String s : split) {
                ss.add(s.trim());
            }
            allSsq.add(ss);
        }
        List<List<String>> oldssq = new ArrayList<>();
        List<Integer> indexList = tjRange();
        System.out.println(indexList);
        for (Integer index : indexList) {
            String str = strs[index - 1];
            String[] split = str.split(",");
            List<String> ss = new ArrayList<>();
            for (String s : split) {
                ss.add(s.trim());
            }
            oldssq.add(ss);
        }

        int[] d = {2, 2, 3};
        int di = 0;
        for (int i = 0; i < 3; i++) {
            printSsq(oldssq, d[di], allSsq);
            di++;
            if (di == 3) {
                di = 0;
            }
            System.out.println("");
        }
    }

    private static void tjRate() throws IOException {
        String ssq = IOUtils.toString(new FileInputStream("D:\\我的\\ssq.txt"), Charset.forName("UTF-8"));
        String[] strs = ssq.split("\n");
        List<List<String>> oldssq = new ArrayList<>();
        for (String str : strs) {
            String[] split = str.split(",");
            List<String> ss = new ArrayList<>();
            int cnt = 1;
            for (String s : split) {
                if (cnt == 7) {
                    break;
                }
                ss.add(s.trim());
                cnt++;
            }
            oldssq.add(ss);
        }
        // 不重复号码数的统计值,key是不重复号码数量,value是统计次数
        Map<Integer, Integer> m = new HashMap<>();
        // 不重复号码距离值的统计数,key是距离,value是统计次数
        Map<Integer, Integer> m2 = new TreeMap<>();
        for (int i = 0; i < oldssq.size(); i++) {
            List<String> old = oldssq.get(i);
            int size = old.size();
            // 最少不重复号码数的号码距离
            int jl = 1;
            for (int i2 = i + 1; i2 < oldssq.size(); i2++) {
                List<String> old2 = oldssq.get(i2);
                List<String> old3 = new ArrayList<>(old);
                old3.removeAll(old2);
                if (size > old3.size()) {
                    size = old3.size();
                    jl = i2 - i;
                }
            }

            if (m.containsKey(size)) {
                m.put(size, m.get(size) + 1);
            } else {
                m.put(size, 1);
            }
            if (m2.containsKey(jl)) {
                m2.put(jl, m2.get(jl) + 1);
            } else {
                m2.put(jl, 1);
            }
        }
        System.out.println(m);
        Map<Integer, List> m3 = new TreeMap<>();
        for (Map.Entry<Integer, Integer> entry : m2.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            if (m3.containsKey(value)) {
                m3.get(value).add(key);
            } else {
                ArrayList list = new ArrayList();
                list.add(key);
                m3.put(value, list);
            }
        }
        System.out.println(m2);
    }

    private static List<Integer> tjRange() throws IOException {
        String ssq = IOUtils.toString(new FileInputStream("D:\\我的\\ssq.txt"), Charset.forName("UTF-8"));
        String[] strs = ssq.split("\n");
        List<List<String>> oldssq = new ArrayList<>();
        for (String str : strs) {
            String[] split = str.split(",");
            List<String> ss = new ArrayList<>();
            for (String s : split) {
                ss.add(s.trim());
            }
            oldssq.add(ss);
        }
        // 不重复号码距离值的统计数,key是距离,value是统计次数
        // 只统计不重复号码数为2和3的距离值
        Map<Integer, Integer> m = new HashMap<>();
        for (int i = 0; i < oldssq.size(); i++) {
            List<String> old = oldssq.get(i);
            int size = old.size();
            int range = 1;
            for (int i2 = i + 1; i2 < oldssq.size(); i2++) {
                List<String> old2 = oldssq.get(i2);
                List<String> old3 = new ArrayList<>(old);
                old3.removeAll(old2);
                if (size > old3.size()) {
                    size = old3.size();
                    range = i2 - i;
                }
            }
            if (i == 0) {
                System.out.println("第一个" + old + ",size:" + size + ", range:" + range);
            }
            if (size == 2 || size == 3) {
                if (m.containsKey(range)) {
                    m.put(range, m.get(range) + 1);
                } else {
                    m.put(range, 1);
                }
            }

        }
        Map<Integer, List> m3 = new TreeMap<>();
        for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            if (m3.containsKey(value)) {
                m3.get(value).add(key);
            } else {
                ArrayList list = new ArrayList();
                list.add(key);
                m3.put(value, list);
            }
        }
//        System.out.println(m3);

        // 获取统计次数大于等5的距离值(也是双色球列表里的索引,用的时候要减1)
        List<Integer> indexList = new ArrayList<>();
        for (Map.Entry<Integer, List> entry : m3.entrySet()) {
            Integer range = entry.getKey();
            if (range >= 5) {
                indexList.addAll(entry.getValue());
            }
        }
        return indexList;
    }

    private static void tjNoRepeat() throws IOException {
        String ssq = IOUtils.toString(new FileInputStream("D:\\我的\\ssq.txt"), Charset.forName("UTF-8"));
        String[] strs = ssq.split("\n");
        List<List<String>> oldssq = new ArrayList<>();
        for (String str : strs) {
            String[] split = str.split(",");
            List<String> ss = new ArrayList<>();
            for (String s : split) {
                ss.add(s.trim());
            }
            oldssq.add(ss);
        }
        // 不重复号码数的统计值,key是不重复号码数量,value是统计次数
        Map<Integer, Integer> m = new HashMap<>();
        for (int i = 0; i < oldssq.size(); i++) {
            List<String> old = oldssq.get(i);
            int size = old.size();
            for (int i2 = i + 1; i2 < oldssq.size(); i2++) {
                List<String> old2 = oldssq.get(i2);
                List<String> old3 = new ArrayList<>(old);
                old3.removeAll(old2);
                if (size > old3.size()) {
                    size = old3.size();
                }
            }

            if (m.containsKey(size)) {
                m.put(size, m.get(size) + 1);
            } else {
                m.put(size, 1);
            }
        }
        System.out.println(m);
    }

    private static void printSsq(List<List<String>> oldssq, int size, List<List<String>> allSsq) {
        List<String> reds = new ArrayList<>();
        for (int i = 1; i <= 33; i++) {
            reds.add(i > 9 ? "" + i : "0" + i);
        }
        // 0-1的随机数
        Set redsOut = new TreeSet();
        for (int i = 0; i < 6; i++) {
            double random = Math.random();
            int index = (int) (reds.size() * random);
            String red = reds.get(index);
            reds.remove(index);
            redsOut.add(red);
        }
        List<String> blues = new ArrayList<>();
        for (int i = 1; i <= 16; i++) {
            blues.add(i > 9 ? "" + i : "0" + i);
        }
        double random = Math.random();
        int index = (int) (blues.size() * random);
        String blue = blues.get(index);

        boolean retry = true;
        for (List<String> old : oldssq) {
            List<String> out2 = new ArrayList<>();
            out2.addAll(redsOut);
            out2.add(blue);
            out2.removeAll(old);
            if (out2.size() == size) {
                retry = checkAll(allSsq, redsOut, blue, size);
            }
            if (out2.size() < size) {
                retry = true;
                break;
            }
        }
        if (retry) {
            printSsq(oldssq, size, allSsq);
            return;
        }

        System.out.print(redsOut.toString().replace("[", "").replace("]", ", "));
        System.out.print(blue);
    }

    /**
     * 检查机选出来的号码和所有号码对比是否存在size更小的号码
     * @param allSsq
     * @param redsOut
     * @param blue
     * @param size
     * @return boolean
     */
    private static boolean checkAll(List<List<String>> allSsq, Set redsOut, String blue, int size) {
        for (List<String> old : allSsq) {
            List<String> out2 = new ArrayList<>();
            out2.addAll(redsOut);
            out2.add(blue);
            out2.removeAll(old);
            if (out2.size() < size) {
                return true;
            }
        }
        return false;
    }
}
相关推荐
qq_12498707536 分钟前
基于Spring Boot的高校实习实践管理系统(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
oak隔壁找我11 分钟前
SpringBoot + MyBatis 配置详解
java·数据库·后端
oak隔壁找我12 分钟前
SpringBoot + Redis 配置详解
java·数据库·后端
躺平的赶海人15 分钟前
C# Dictionary 线程安全指南:多线程下操作 Dictionary<string, DateTime> 的加锁策略
java·安全·c#
帧栈24 分钟前
开发避坑指南(64):修复IllegalArgumentException:参数值类型与期望类型不匹配
java·数据库
坐不住的爱码25 分钟前
ArrayList和LinkedList的区别
java
java1234_小锋30 分钟前
什么是Java三高架构?
java·开发语言·架构
浓墨染彩霞44 分钟前
Java----set
java·经验分享·笔记
5pace1 小时前
【JavaWeb|第一篇】Maven篇
java·maven
Code_Geo1 小时前
agent设计模式:第三章节—并行化
java·设计模式·agent·并行化