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;
    }
}
相关推荐
陌上花开࿈1 小时前
调用第三方接口
java
Aileen_0v01 小时前
【玩转OCR | 腾讯云智能结构化OCR在图像增强与发票识别中的应用实践】
android·java·人工智能·云计算·ocr·腾讯云·玩转腾讯云ocr
桂月二二3 小时前
Java与容器化:如何使用Docker和Kubernetes优化Java应用的部署
java·docker·kubernetes
liuxin334455663 小时前
学籍管理系统:实现教育管理现代化
java·开发语言·前端·数据库·安全
小马爱打代码3 小时前
设计模式详解(建造者模式)
java·设计模式·建造者模式
栗子~~4 小时前
idea 8年使用整理
java·ide·intellij-idea
2301_801483694 小时前
Maven核心概念
java·maven
Q_19284999064 小时前
基于Spring Boot的电影售票系统
java·spring boot·后端
我要学编程(ಥ_ಥ)5 小时前
初始JavaEE篇 —— 网络原理---传输层协议:深入理解UDP/TCP
java·网络·tcp/ip·udp·java-ee
就爱学编程5 小时前
重生之我在异世界学编程之C语言:数据在内存中的存储篇(下)
java·服务器·c语言