定义
编辑运算
编辑Ax桅 =桅 , 桅 xA=桅
案例
编辑代码
编辑C#源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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]); } } } } } |
使用说明
List<string> result = new List<string>();
Descartes.run(dimvalue, result, 0, "");
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import java.util.ArrayList; import java.util.List; //import com.alibaba.fastjson.JSON; public class DescartesUtil { public static void main(String[] args) { List<List<String>> list = new ArrayList<List<String>>(); List<String> listSub1 = new ArrayList<String>(); List<String> listSub2 = new ArrayList<String>(); List<String> listSub3 = new ArrayList<String>(); List<String> listSub4 = new ArrayList<String>(); listSub1.add( "1" ); listSub1.add( "2" ); listSub2.add( "3"
|