以下是使用C#编程语言来得到有四个小球,有放回地抽取18次的所有结果的示例代码:

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        int[] balls = { 1, 2, 3, 4 }; // 四个小球的编号

        List<List<int>> results = GetAllResults(balls, 18);

        foreach (var result in results)
        {
            Console.WriteLine(string.Join(", ", result));
        }
    }

    static List<List<int>> GetAllResults(int[] balls, int times)
    {
        List<List<int>> results = new List<List<int>>();
        List<int> currentResult = new List<int>();

        GetResultsRecursive(balls, times, currentResult, results);

        return results;
    }

    static void GetResultsRecursive(int[] balls, int times, List<int> currentResult, List<List<int>> results)
    {
        if (times == 0)
        {
            results.Add(new List<int>(currentResult));
            return;
        }

        foreach (var ball in balls)
        {
            currentResult.Add(ball);
            GetResultsRecursive(balls, times - 1, currentResult, results);
            currentResult.RemoveAt(currentResult.Count - 1);
        }
    }
}

此代码通过递归实现了获取有四个小球,有放回地抽取18次的所有结果的功能。它将每个结果存储在一个二维列表中,并在主函数中将结果打印出来。输出的结果将是所有可能的组合,每行表示一个结果,其中每个数字表示抽取到的小球的编号

用C#语言得到有四个小球有放回的抽取18次的所有结果

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

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