代码之家  ›  专栏  ›  技术社区  ›  NullPointerException

创建选择多个彩票号码的算法(数学和统计学)

  •  -2
  • NullPointerException  · 技术社区  · 7 年前

    在西班牙的赌博系统中有一个概念叫做 multiple 这意味着如果你想玩的游戏有6个数字的赌注,你可以创建一个7或8个数字的特殊赌注,甚至9,10或11个数字。这种特殊的赌注将在X正常赌注6个数字将结合给定的数字。

    The multiple bet of 7 numbers will traduce in 7 bets of 6 numbers. 
    The multiple bet of 8 numbers will traduce in 28 bets of 6 numbers.
    The multiple bet of 9 numbers will traduce in 84 bets of 6 numbers.
    The multiple bet of 10 numbers will traduce in 210 bets of 6 numbers.
    The multiple bet of 11 numbers will traduce in 462 bets of 6 numbers.
    

    234567 134567 124567 123567 123467 123457 123456

    数字为1,2,3,4,5,6,7,8的8的倍数示例:

    123456 123457 123458 123467 123468 123478 123567 123568 123578 123678 124567 124568 124578 124678 125678 134567 134568 134578 134678 135678 145678 234567 234568 234578 234678 235678 245678 345678

    我的第一个目标是用Java实现一个用于生成的算法 multiples . 我的意思是,每次下注的成本是1个硬币,所以,举个例子,30个数字和800个硬币,把800个硬币浪费在X个数字的X倍下注中。多次下注必须将30个数字组合在一起,以或多或少相等的数量出现。

    总成本的倍数必须接近800欧元,可以少一点,但决不能超过800欧元。 我不知道如何做到这一点。

    http://www.miramiprimi.miraestudio.es/MetodoMultiplePrimitiva.php

    1 回复  |  直到 6 年前
        1
  •  2
  •   dbl    7 年前

    @NullPointerException我今天早些时候的意思是,应用于给定URL的算法几乎是一个暴力算法,用于生成N个符号集中长度为6的所有组合。它的代码非常简单:

    private static List<List<Integer>> bruteForce(List<Integer> numbers) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> current;
        for (int position1 = 0; position1 < numbers.size(); position1++) {
            for (int position2 = position1 + 1; position2 < numbers.size(); position2++) {
                for (int position3 = position2 + 1; position3 < numbers.size(); position3++) {
                    for (int position4 = position3 + 1; position4 < numbers.size(); position4++) {
                        for (int position5 = position4 + 1; position5 < numbers.size(); position5++) {
                            for (int position6 = position5 + 1; position6 < numbers.size(); position6++) {
                                current = new ArrayList<>();
    
                                current.add(numbers.get(position1));
                                current.add(numbers.get(position2));
                                current.add(numbers.get(position3));
                                current.add(numbers.get(position4));
                                current.add(numbers.get(position5));
                                current.add(numbers.get(position6));
    
                                result.add(current);
                            }
                        }
                    }
                }
            }
        }
        return result;
    }
    

    还有一些用例://请记住它适用于array\u length>=6.

    public static void main(String[] args) {
        List<Integer> numbers;
    
        System.out.println("N = 6, resulting in 1 element in the list.");
        numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6);}};
        bruteForce(numbers).forEach(System.out::println);
    
        System.out.println("N = 7, resulting in 7 element in the list.");
        numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7);}};
        bruteForce(numbers).forEach(System.out::println);
    
        System.out.println("N = 8, resulting in 28 element in the list.");
        numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8);}};
        bruteForce(numbers).forEach(System.out::println);
    
        System.out.println("N = 9, resulting in 84 element in the list.");
        numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8); add(9);}};
        bruteForce(numbers).forEach(System.out::println);
    }
    

    N = 6, resulting in 1 element in the list.
    [1, 2, 3, 4, 5, 6]
    N = 7, resulting in 7 element in the list.
    [1, 2, 3, 4, 5, 6]
    [1, 2, 3, 4, 5, 7]
    [1, 2, 3, 4, 6, 7]
    [1, 2, 3, 5, 6, 7]
    [1, 2, 4, 5, 6, 7]
    [1, 3, 4, 5, 6, 7]
    [2, 3, 4, 5, 6, 7]
    N = 8, resulting in 28 element in the list.
    [1, 2, 3, 4, 5, 6]
    [1, 2, 3, 4, 5, 7]
    [1, 2, 3, 4, 5, 8]
    [1, 2, 3, 4, 6, 7]
    [1, 2, 3, 4, 6, 8]
    [1, 2, 3, 4, 7, 8]
    [1, 2, 3, 5, 6, 7]
    [1, 2, 3, 5, 6, 8]
    [1, 2, 3, 5, 7, 8]
    [1, 2, 3, 6, 7, 8]
    [1, 2, 4, 5, 6, 7]
    [1, 2, 4, 5, 6, 8]
    [1, 2, 4, 5, 7, 8]
    [1, 2, 4, 6, 7, 8]
    [1, 2, 5, 6, 7, 8]
    [1, 3, 4, 5, 6, 7]
    [1, 3, 4, 5, 6, 8]
    [1, 3, 4, 5, 7, 8]
    [1, 3, 4, 6, 7, 8]
    [1, 3, 5, 6, 7, 8]
    [1, 4, 5, 6, 7, 8]
    [2, 3, 4, 5, 6, 7]
    [2, 3, 4, 5, 6, 8]
    [2, 3, 4, 5, 7, 8]
    [2, 3, 4, 6, 7, 8]
    [2, 3, 5, 6, 7, 8]
    [2, 4, 5, 6, 7, 8]
    [3, 4, 5, 6, 7, 8]
    N = 9, resulting in 84 element in the list.
    [1, 2, 3, 4, 5, 6]
    [1, 2, 3, 4, 5, 7]
    [1, 2, 3, 4, 5, 8]
    [1, 2, 3, 4, 5, 9]
    [1, 2, 3, 4, 6, 7]
    [1, 2, 3, 4, 6, 8]
    [1, 2, 3, 4, 6, 9]
    [1, 2, 3, 4, 7, 8]
    [1, 2, 3, 4, 7, 9]
    [1, 2, 3, 4, 8, 9]
    [1, 2, 3, 5, 6, 7]
    [1, 2, 3, 5, 6, 8]
    [1, 2, 3, 5, 6, 9]
    [1, 2, 3, 5, 7, 8]
    [1, 2, 3, 5, 7, 9]
    [1, 2, 3, 5, 8, 9]
    [1, 2, 3, 6, 7, 8]
    [1, 2, 3, 6, 7, 9]
    [1, 2, 3, 6, 8, 9]
    [1, 2, 3, 7, 8, 9]
    [1, 2, 4, 5, 6, 7]
    [1, 2, 4, 5, 6, 8]
    [1, 2, 4, 5, 6, 9]
    [1, 2, 4, 5, 7, 8]
    [1, 2, 4, 5, 7, 9]
    [1, 2, 4, 5, 8, 9]
    [1, 2, 4, 6, 7, 8]
    [1, 2, 4, 6, 7, 9]
    [1, 2, 4, 6, 8, 9]
    [1, 2, 4, 7, 8, 9]
    [1, 2, 5, 6, 7, 8]
    [1, 2, 5, 6, 7, 9]
    [1, 2, 5, 6, 8, 9]
    [1, 2, 5, 7, 8, 9]
    [1, 2, 6, 7, 8, 9]
    [1, 3, 4, 5, 6, 7]
    [1, 3, 4, 5, 6, 8]
    [1, 3, 4, 5, 6, 9]
    [1, 3, 4, 5, 7, 8]
    [1, 3, 4, 5, 7, 9]
    [1, 3, 4, 5, 8, 9]
    [1, 3, 4, 6, 7, 8]
    [1, 3, 4, 6, 7, 9]
    [1, 3, 4, 6, 8, 9]
    [1, 3, 4, 7, 8, 9]
    [1, 3, 5, 6, 7, 8]
    [1, 3, 5, 6, 7, 9]
    [1, 3, 5, 6, 8, 9]
    [1, 3, 5, 7, 8, 9]
    [1, 3, 6, 7, 8, 9]
    [1, 4, 5, 6, 7, 8]
    [1, 4, 5, 6, 7, 9]
    [1, 4, 5, 6, 8, 9]
    [1, 4, 5, 7, 8, 9]
    [1, 4, 6, 7, 8, 9]
    [1, 5, 6, 7, 8, 9]
    [2, 3, 4, 5, 6, 7]
    [2, 3, 4, 5, 6, 8]
    [2, 3, 4, 5, 6, 9]
    [2, 3, 4, 5, 7, 8]
    [2, 3, 4, 5, 7, 9]
    [2, 3, 4, 5, 8, 9]
    [2, 3, 4, 6, 7, 8]
    [2, 3, 4, 6, 7, 9]
    [2, 3, 4, 6, 8, 9]
    [2, 3, 4, 7, 8, 9]
    [2, 3, 5, 6, 7, 8]
    [2, 3, 5, 6, 7, 9]
    [2, 3, 5, 6, 8, 9]
    [2, 3, 5, 7, 8, 9]
    [2, 3, 6, 7, 8, 9]
    [2, 4, 5, 6, 7, 8]
    [2, 4, 5, 6, 7, 9]
    [2, 4, 5, 6, 8, 9]
    [2, 4, 5, 7, 8, 9]
    [2, 4, 6, 7, 8, 9]
    [2, 5, 6, 7, 8, 9]
    [3, 4, 5, 6, 7, 8]
    [3, 4, 5, 6, 7, 9]
    [3, 4, 5, 6, 8, 9]
    [3, 4, 5, 7, 8, 9]
    [3, 4, 6, 7, 8, 9]
    [3, 5, 6, 7, 8, 9]
    [4, 5, 6, 7, 8, 9]
    

    请记住,输出中使用的数字来自 List<Integer> numbers = ... 所以你可以玩一玩,进一步测试。我还建议在这个例子中使用有序集而不是列表。

    numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};
    bruteForce(numbers).forEach(System.out::println);
    

    印刷品:

    [11, 22, 33, 44, 45, 46]
    [11, 22, 33, 44, 45, 47]
    [11, 22, 33, 44, 46, 47]
    [11, 22, 33, 45, 46, 47]
    [11, 22, 44, 45, 46, 47]
    [11, 33, 44, 45, 46, 47]
    [22, 33, 44, 45, 46, 47]
    

    enter image description here

    我将为您添加非常相同的方法的递归实现,该方法也将以基(子集长度)作为输入参数。

    private static List<List<Integer>> bruteForceRecursive(List<Integer> numbers, List<Integer> indexes, int base) {
        List<List<Integer>> result = new ArrayList<>();
    
        if (indexes.size() == base) {
            List<Integer> list = new ArrayList<>();
    
            indexes.forEach(x -> list.add(numbers.get(x)));
            result.add(list);
    
            return result;
        }
    
        for (int i = indexes.isEmpty() ? 0 : indexes.get(indexes.size() - 1) + 1; i < numbers.size(); i++) {
            indexes.add(i);
            result.addAll(bruteForceRecursive(numbers, indexes, base));
            indexes.remove(indexes.size() - 1);
        }
    
        return result;
    }
    

    然后是用法示例:

    public static void main(String[] args) {
        List<Integer> numbers;
    
        numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};
    
        bruteForceRecursive(numbers, new ArrayList<>(), 2).forEach(System.out::println);
        bruteForceRecursive(numbers, new ArrayList<>(), 4).forEach(System.out::println);
        bruteForceRecursive(numbers, new ArrayList<>(), 6).forEach(System.out::println);
    }
    

    打印:

    [11, 22]
    [11, 33]
    [11, 44]
    [11, 45]
    [11, 46]
    [11, 47]
    [22, 33]
    [22, 44]
    [22, 45]
    [22, 46]
    [22, 47]
    [33, 44]
    [33, 45]
    [33, 46]
    [33, 47]
    [44, 45]
    [44, 46]
    [44, 47]
    [45, 46]
    [45, 47]
    [46, 47]
    [11, 22, 33, 44]
    [11, 22, 33, 45]
    [11, 22, 33, 46]
    [11, 22, 33, 47]
    [11, 22, 44, 45]
    [11, 22, 44, 46]
    [11, 22, 44, 47]
    [11, 22, 45, 46]
    [11, 22, 45, 47]
    [11, 22, 46, 47]
    [11, 33, 44, 45]
    [11, 33, 44, 46]
    [11, 33, 44, 47]
    [11, 33, 45, 46]
    [11, 33, 45, 47]
    [11, 33, 46, 47]
    [11, 44, 45, 46]
    [11, 44, 45, 47]
    [11, 44, 46, 47]
    [11, 45, 46, 47]
    [22, 33, 44, 45]
    [22, 33, 44, 46]
    [22, 33, 44, 47]
    [22, 33, 45, 46]
    [22, 33, 45, 47]
    [22, 33, 46, 47]
    [22, 44, 45, 46]
    [22, 44, 45, 47]
    [22, 44, 46, 47]
    [22, 45, 46, 47]
    [33, 44, 45, 46]
    [33, 44, 45, 47]
    [33, 44, 46, 47]
    [33, 45, 46, 47]
    [44, 45, 46, 47]
    [11, 22, 33, 44, 45, 46]
    [11, 22, 33, 44, 45, 47]
    [11, 22, 33, 44, 46, 47]
    [11, 22, 33, 45, 46, 47]
    [11, 22, 44, 45, 46, 47]
    [11, 33, 44, 45, 46, 47]
    [22, 33, 44, 45, 46, 47]