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;
    }
}
相关推荐
喵叔哟8 分钟前
重构代码之移动字段
java·数据库·重构
喵叔哟8 分钟前
重构代码之取消临时字段
java·前端·重构
fa_lsyk11 分钟前
maven环境搭建
java·maven
Daniel 大东30 分钟前
idea 解决缓存损坏问题
java·缓存·intellij-idea
wind瑞36 分钟前
IntelliJ IDEA插件开发-代码补全插件入门开发
java·ide·intellij-idea
HappyAcmen36 分钟前
IDEA部署AI代写插件
java·人工智能·intellij-idea
马剑威(威哥爱编程)42 分钟前
读写锁分离设计模式详解
java·设计模式·java-ee
鸽鸽程序猿43 分钟前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
修道-032343 分钟前
【JAVA】二、设计模式之策略模式
java·设计模式·策略模式
九圣残炎1 小时前
【从零开始的LeetCode-算法】2559. 统计范围内的元音字符串数
java·算法·leetcode