首页 新闻 赞助 找找看

多个数组中选择元素组成新数组问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2011-06-22 12:44

问题:

1、数组个数(2-6)是变化的。

2、数组中元素个数(1-3)也是变化的。

如:

数组1{1,2,3}

数组2{4,5,6}

数组3{7,8,9}

现在要从前面3个数组中选择元素组成由两个元素组成的新二维数组

如:

{1,4},{1,5},{1,6},{1,7},{1,8},{1,9}...

问题补充:
点击查看代码
1 public static string Translate(string type, string str)
2 {
3 StringBuilder sb = new StringBuilder();
4 IList<int> typeList = AnalyzeType(type);
5 foreach (int numType in typeList)
6 {
7 BuildResult(numType, AnalyzeStr(str), sb);
8 }
9 return sb.ToString() + "\n" + (sb.ToString().Split('+').Length - 1).ToString();
10 }
11 public static IList<int> AnalyzeType(string type)
12 {
13 IList<int> typeList = new List<int>();
14 Regex reg = new Regex(@"^\d{1}$");
15 Array arr = type.ToArray();
16 foreach (var character in arr)
17 {
18 if (reg.IsMatch(character.ToString()))
19 {
20 typeList.Add(int.Parse(character.ToString()));
21 }
22 }
23 return typeList;
24 }
25
26 public static IList<string> AnalyzeStr(string str)
27 {
28 IList<string> list = new List<string>();
29 Regex reg = new Regex(@"^\d{6}$");
30 string[] content = str.Split(',');
31 foreach (string everyStr in content)
32 {
33 if (reg.IsMatch(everyStr))
34 {
35 list.Add(everyStr);
36 }
37 }
38 return list;
39 }
40
41 public static void BuildResult(int type, IList<string> contentList, StringBuilder sb)
42 {
43 if (contentList.Count != 0)
44 {
45 List<string> li = GetCombinationF(contentList, type);
46 foreach (string str in li)
47 {
48 string[] strArray = str.Split(',');
49 if (hasSame(strArray))
50 continue;
51 else
52 {
53 sb.Append(str).Append("+");
54 }
55 }
56 }
57 }
58
59 public static bool hasSame(string[] strArray)
60 {
61 string start = string.Empty;
62 for (int i = 0; i < strArray.Length; i++)
63 {
64 start = strArray[i].Substring(0, 3).ToString();
65 for (int j = i + 1; j < strArray.Length; j++)
66 {
67 if (start == strArray[j].Substring(0, 3))
68 return true;
69 }
70 }
71 return false;
72 }
73
74
75 #region 组合算法(非递归方法)
76 public static List<string> GetCombinationF(IList<string> strArray, int selectCount)
77 {
78 int totalCount = strArray.Count;
79 int[] currentSelect = new int[selectCount];
80 int last = selectCount - 1;
81 List<string> output = new List<string>();
82 string s;
83
84 //付初始值
85 for (int i = 0; i < selectCount; i++)
86 currentSelect[i] = i;
87
88 while (true)
89 {
90 s = "";
91 //输出部分,生成的时候从0计数,所以输出的时候+1
92 for (int i = 0; i < selectCount; i++)
93 {
94 if ((selectCount - i) == 1)
95 s += strArray[currentSelect[i]];
96 else
97 s += strArray[currentSelect[i]] + ",";
98 }
99 output.Add(s);
100
101 //如果不进位
102 if (currentSelect[last] < totalCount - 1)
103 currentSelect[last]++;
104 else
105 {
106 //进位部分
107 int position = last;
108
109 while (position > 0 && currentSelect[position - 1] == currentSelect[position] - 1)
110 position--;
111
112 if (position == 0)
113 break;
114
115 currentSelect[position - 1]++;
116
117 for (int i = position; i < selectCount; i++)
118 currentSelect[i] = currentSelect[i - 1] + 1;
119 }
120 }
121 return output;
122 }
123 #endregion
01之间穿梭的主页 01之间穿梭 | 初学一级 | 园豆:25
提问于:2011-05-02 19:23
< >
分享
所有回答(2)
0

怎么个意思?不是很明白

笨晓孩 | 园豆:272 (菜鸟二级) | 2011-05-03 14:26
0

声明这么多的二位数组有啥用呢?

like%'远远'% | 园豆:635 (小虾三级) | 2011-05-03 16:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册