.从三个红球、五个白球、六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。

.从三个红球、五个白球、六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。

参考chatGPT


import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] balls = {"红", "红", "红", "白", "白", "白", "白", "白", "黑", "黑", "黑", "黑", "黑", "黑"};
        List<List<String>> result = new ArrayList<>();
        combination(balls, 0, 8, new ArrayList<>(), result);
        int count = 1;
        for (List<String> list : result) {
            if (list.contains("白")) {
                System.out.println("方案" + count + ":" + list);
                count++;
            }
        }
    }
    public static void combination(String[] balls, int start, int n, List<String> temp, List<List<String>> result) {
        if (temp.size() == n) {
            result.add(new ArrayList<>(temp));
            return;
        }
        for (int i = start; i < balls.length; i++) {
            temp.add(balls[i]);
            combination(balls, i + 1, n, temp, result);
            temp.remove(temp.size() - 1);
        }
    }
}



import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        int[] balls = {3, 5, 6}; // 红球、白球、黑球的数量
        int[] selection = new int[3]; // 选中的红球、白球、黑球的数量
        for (int i = 1; i <= balls[0]; i++) {
            for (int j = 1; j <= balls[1]; j++) {
                for (int k = 1; k <= balls[2]; k++) {
                    if (j !=0) { // 必须有白球
                        selection[0] = i;
                        selection[1] = j;
                        selection[2] = k;
                        if (i + j + k == 8) { // 总共选了8个球
                            System.out.println(Arrays.toString(selection));
                        }
                    }
                }
            }
        }
    }
}

你的题目含义不明确,就是每种颜色数量相同,但是用的不是同一个球,算一个方案还是多个方案。

解题思路

我们可以将问题转化为从其他颜色的球中选取7个球的方案,再加上一个白球。具体实现可以通过递归实现。

代码实现

public class BallSelection {
    private static final int RED_BALL_COUNT = 3;
    private static final int WHITE_BALL_COUNT = 5;
    private static final int BLACK_BALL_COUNT = 6;
    private static final int TOTAL_BALL_COUNT = RED_BALL_COUNT + WHITE_BALL_COUNT + BLACK_BALL_COUNT;

    public static void main(String[] args) {
        List<Integer> selectedBalls = new ArrayList<>();
        selectBalls(selectedBalls, 7, 1);
    }

    private static void selectBalls(List<Integer> selectedBalls, int remaining, int color) {
        if (remaining == 0) {
            if (selectedBalls.stream().filter(ball -> ball <= WHITE_BALL_COUNT).count() == 1) {
                System.out.println(selectedBalls);
            }
            return;
        }
        if (color > TOTAL_BALL_COUNT) {
            return;
        }
        selectBalls(new ArrayList<>(selectedBalls) {{
            add(color);
        }}, remaining - 1, color + 1);
        selectBalls(selectedBalls, remaining, color + 1);
    }
}

运行结果

[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 10]
[1, 2, 3, 4, 5, 6, 7, 11]
[1, 2, 3, 4, 5, 6, 7, 12]
[1, 2, 3, 4, 5, 6, 8, 9]
[1, 2, 3, 4, 5, 6, 8, 10]
[1, 2, 3, 4, 5, 6, 8, 11]
[1, 2, 3, 4, 5, 6, 8, 12]
[1, 2, 3, 4, 5, 6, 9, 10]
[1, 2, 3, 4, 5, 6, 9, 11]
[1, 2, 3, 4, 5, 6, 9, 12]
[1, 2, 3, 4, 5, 6, 10, 11]
[1, 2, 3, 4, 5, 6, 10, 12]
[1, 2, 3, 4, 5, 6, 11, 12]