赛马问题11次验证

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        for (int n = 0; n < 100000; n++) {
            
            //保存初始64匹🐎
            ArrayList<Double> horses64 = new ArrayList<Double>(); 
            Random r = new Random();
            //随机生成64匹🐎
            for (int i = 0; i < 64; i++) {
                horses64.add(r.nextDouble());
            }
            //保存分成8组后的8*8=64匹🐎
            ArrayList<ArrayList<Double>> groups8 = new ArrayList<ArrayList<Double>>();
            //用于保存8组中的第一名🐎
            ArrayList<Double> horses8 = new ArrayList<Double>();
            
            for (int i = 0; i < 8; i++) {
                groups8.add(new ArrayList<Double>());
                for (int j = 0; j < 8; j++) {
                    groups8.get(i).add(horses64.get(i * 8 + j));
                }
                //前8次赛跑
                Collections.sort(groups8.get(i));
                horses8.add(groups8.get(i).get(0));
            }
            //第9次赛跑:8组中每组第一名一起跑
            Collections.sort(horses8);
            //挑选出具有前四可能的最后10匹🐎
            ArrayList<Double> horses10 = new ArrayList<Double>();
            for (int i = 0; i < 4; i++) {
                int group_number = 0;
                for (int j = 0; j < 8; j++) {
                    if (groups8.get(j).get(0) == horses8.get(i)) {
                        group_number = j;
                    }
                }
                for (int j = 0; j < 4 - i; j++) {
                    horses10.add(groups8.get(group_number).get(j));
                }
            }
            //第10、11次赛跑可从10匹🐎中决出前四名,这里简化一次出结果,赛🐎完成
            Collections.sort(horses10);
//计算真实排名 Collections.sort(horses64); //比较前四名,验证正确性 for (int i = 0; i < 4; i++) { if (horses10.get(i) != horses64.get(i)) { System.out.println("error"); return; } } } } }

结果:循环10w组随机数据,均验证无误

posted @ 2020-08-06 22:44  whatskd  阅读(84)  评论(0)    收藏  举报