解决方案 »

  1.   

    using System;using System.Collections;using System.Collections.Generic;using System.Text;using System.Linq;public class Descartes{public static void run(List<List<string>> dimvalue, List<string> result, int layer, 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]);}}}}}程序使用说明(1)将每个维度的集合的元素视为List<string>,多个集合构成List<List<string>> dimvalue作为输入(2)将多维笛卡尔乘积的结果放到List<string> result之中作为输出(3)int layer, string curstring只是两个中间过程的参数携带变量(4)程序采用递归调用,起始调用示例如下:List<string> result = new List<string>();Descartes.run(dimvalue, result, 0, "");即可获得多维笛卡尔乘积的结果。