DotNet 笔记

东方欲晓,莫道君起早。

C#笛卡尔乘积的计算

笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class Descartes
{
    /// <summary>
    /// 笛卡尔积
    /// </summary>
    /// <param name="dimvalue">将每个维度的集合的元素视为List<string>,多个集合构成List<List<string>> dimvalue作为输入</param>
    /// <param name="result">将多维笛卡尔乘积的结果放到List<string> result之中作为输出</param>
    /// <param name="layer">int layer 只是两个中间过程的参数携带变量</param>
    /// <param name="curstring"> string curstring只是两个中间过程的参数携带变量,传递""就行</param>
    public static void run(List<List<string>> dimvalue, List<string> result, int layer=0, string curstring="")
    {
        if (layer < dimvalue.Count - 1)
        {
            if (dimvalue[layer].Count == 0)
                run(dimvalue, result, layer + 1, curstring);
            else
            {
                for (int i = 0; i < dimvalue[layer].Count; i++)
                {
                    StringBuilder s1 = new StringBuilder();
                    s1.Append(curstring);
                    s1.Append(dimvalue[layer][i]);
                    run(dimvalue, result, layer + 1, s1.ToString());
                }
            }
        }
        else if (layer == dimvalue.Count - 1)
        {
            if (dimvalue[layer].Count == 0) result.Add(curstring);
            else
            {
                for (int i = 0; i < dimvalue[layer].Count; i++)
                {
                    result.Add(curstring + dimvalue[layer][i]);
                }
            }
        }
    }
}

 

Loading