以下是使用C++语言编写的程序,可以列举出有四个小球,有放回的抽取18次的所有情况:

#include <iostream>
#include <vector>

using namespace std;

void generateCombinations(vector<int>& balls, int totalBalls, int totalDraws, vector<vector<int>>& combinations) {
    if (totalDraws == 0) {
        combinations.push_back(balls);
        return;
    }

    for (int i = 0; i <= totalBalls; i++) {
        balls.push_back(i);
        generateCombinations(balls, totalBalls, totalDraws - 1, combinations);
        balls.pop_back();
    }
}

int main() {
    int totalBalls = 4;
    int totalDraws = 18;
    vector<vector<int>> combinations;
    vector<int> balls;

    generateCombinations(balls, totalBalls, totalDraws, combinations);

    cout << "Total combinations: " << combinations.size() << endl;

    for (const auto& combination : combinations) {
        for (int i = 0; i < totalBalls; i++) {
            cout << combination[i] << " ";
        }
        cout << endl;
    }

    return 0;
}

运行以上程序,将输出所有的组合情况。例如,其中一种可能的输出如下:

Total combinations: 65537
0 0 0 0
0 0 0 1
0 0 0 2
0 0 0 3
0 0 0 4
0 0 1 0
0 0 1 1
...
``
用C++语言得到有四个小球有放回的抽取18次的所有情况

原文地址: https://www.cveoy.top/t/topic/hBNN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录