List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5");
假设有4个List。已经放入测试数据。我想生产一种组合。组合的要求:1.组合生成出来的格式,我用字符串来表示:"测试数据A1,测试数据B1,测试数据C1,测试数据D1"2.第一点的格式说明了,分别从每个List里面取出数据,只能取出一个。那么表明,每个List里面的数据之间具有互斥关系。同时只能出现一个。3.但是我也有可能不需要第一个List的数据,那么,生成的组合可能就是"测试数据B1,测试数据C1,测试数据D1"4.以上说明,我可能从List里面取出数据,也可能不取出,要是取出的,只能取出List的一个数据。5.注意:"测试数据A1,测试数据B1" 和 "测试数据B1,测试数据A1" 属于一个组合。
不知道大家明白了我的意思没。生成出来的组合我就写几个:"测试数据A1""测试数据B1,测试数据C1""测试数据A1,测试数据B1,测试数据C1"
大家帮忙下咯!TKS,
解决方案 »
- http持续请求
- C#写的小程序由于有IO操作,放共享文件夹中无法直接运行,必须拷贝到本机才不出错.求解
- 请教工作中的两个疑惑
- 如何将信息存入文件中
- C#调用DLL
- 高手们帮忙,怎么在水晶报表中设置曲线图的数据.我用的是vs2008自带的cr.
- GridView合并里面的单元格,怎么做?
- *****************菜鸟求助:如何将Aceess中的所有表(多个表)的表名得到?**********
- DataGridView dg = new DataGridView();请问dg若换成变量,应该怎么改写?
- 一个很简单的问题
- C#中C/S模式下的报表怎么制作,请高人指点,要能动态的添加字段,删选条件,比较灵活的那种,用代码控制的,麻烦了?????????????
- Mysql 问题
2. 设置一个随机数number, 用number%2的余数判断是否取该List的表中数据
3. 要是取就去一个放在一个string里面,不取转到下一个
4. 重复23,直到取到List4中的数据
1 2 3 4
需要先求这四个数的所有组合吧比如有个是 1 3
然后再用 1 3 的item来组成字符串,即一个结果
可以满足你所有要求,而且没有:"测试数据A1,测试数据B1" 和 "测试数据B1,测试数据A1"重复。但会出现:"测试数据A1,测试数据B1" 和 "测试数据A1,测试数据B1"的相同组合,你去出来以后去判断有没有这个字符串就可以避免这种现象了
1 2 3 4如果他们组合,可能是4个List可能只有其中一个List组合,或者可能有其中两个List组合,或者其中的3个,4个。因为,我可能不需要其中一个list的数据。
选两个list就在list1里面循环list2 在list2里面输出你要的格式数据
选3个list就list1里面循环list2 在list2里面循环list3 在list3里面输出你要的格式
选4个仿照上面的就行
不知道我说的可行不
然后从0001到1111循环,每次循环里面再加一层循环0-4
伪代码:
list<list<string>> ret;
for j =0 to 4
list<string> tmp
for i=0001 to 1111
if (i 按位与 1000 ) ==0 则 continue;
else if(存在(列表i[j])) ret.add(列表i[j])
end for
ret.add(tmp)
end for
return ret;
也就是你要的数据如果是三个list应该是list1的长度乘以list2的长度乘以list3的长度
是输出所有的组合吗?如果是的话,我觉得用两个循环就可以了吧,内部循环类似这样:
for (int i = 0 ; i ++; i < a.count + 1){
if (i == i.count) 不输出
else 输出。
}
arr.Add(lista);
arr.Add(listb);
arr.Add(listc);
arr.Add(listd);string s1 = "";
foreach(List<string> lst in arr){
for(int i = 0;i ++ ; i < lst.length + 1){
if (i < lst.length) {
s1 += lst[i];
}
}
s1 += "\r\n";
}s1就列出了所有的组合。
List<string> listShow = new List<string>();
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
List<string> list4 = new List<string>(); //选2个
foreach (string list1Str in list1)
{
foreach (string list2Str in list2)
{
listShow.Add(list1Str + "," + list2Str);
}
}
//选3个
foreach (string list1Str in list1)
{
foreach (string list2Str in list2)
{
foreach (string list3Str in list3)
{
listShow.Add(list1Str + "," + list2Str + "," + list3);
}
}
}
//选4个
foreach (string list1Str in list1)
{
foreach (string list2Str in list2)
{
foreach (string list3Str in list3)
{
foreach (string list4Str in list4)
{
listShow.Add(list1Str + "," + list2Str + "," + list3 + "," + list4);
}
}
}
}
//选2个
foreach (string list1Str in list1)
{
foreach (string list2Str in list2)
{
listShow.Add(list1Str + "," + list2Str);
}
}
这里列出了list1 + list2的数据,,但list1 + list3 之类的就丢失了。
我是要输出所有所有的组合。分别从List里面取一个数据出来,如果不取,可以理解为取一null
这摆明了是排列问题.
LZ是要求出规则内所有可能的排列呢?
还是先指定那些List可取然后随机抽取测试数据最后求出这些数据的所有可能排列呢?
public static List<String> GetMerge(List<String> l1, List<String> l2)
{
List<String> list = new List<String>(); for (Int32 i = 0; i < l1.Count; i++)
{
for (Int32 j = 0; j < l2.Count; j++)
{
list.Add(l1[i] + "," + l2[j]);
}
}
return list;
}
static void Main(string[] args)
{
List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5"); List<String> result = new List<String>(); for (Int32 i = 1; i < 16; i++)
{
List<List<String>> tempL = new List<List<String>>(); if ((i & 1) == 1)
{
tempL.Add(lista);
}
if ((i & 2) == 2)
{
tempL.Add(listb);
}
if ((i & 4) == 4)
{
tempL.Add(listc);
}
if ((i & 8) == 8)
{
tempL.Add(listd);
} if (tempL.Count == 1)
{
result.AddRange(tempL[0]);
}
else if (tempL.Count == 2)
{
result.AddRange(GetMerge(tempL[0], tempL[1]));
}
else if (tempL.Count == 3)
{
result.AddRange(GetMerge(GetMerge(tempL[0], tempL[1]), tempL[2]));
}
else
{
result.AddRange(GetMerge(GetMerge(tempL[0], tempL[1]), GetMerge(tempL[2], tempL[3])));
}
} foreach (String s in result)
Console.WriteLine(s);
我举例是4个List,可能还有很多。我排列出来的组合,可能只是一种一个list中提取,或者2个。
假设我还有很多List,那怎么办-,-/////
public static List<String> GetMerge(List<String> l1, List<String> l2)
{
List<String> list = new List<String>(); for (Int32 i = 0; i < l1.Count; i++)
{
for (Int32 j = 0; j < l2.Count; j++)
{
list.Add(l1[i] + "," + l2[j]);
}
}
return list;
} static void Main(string[] args)
{ List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5"); List<String> liste = new List<string>();
liste.Add("测试数据Peter"); List<List<String>> source = new List<List<String>>();
source.Add(lista);
source.Add(listb);
source.Add(listc);
source.Add(listd);
source.Add(liste); List<String> result = new List<String>(); for (Int32 i = 1; i < Math.Pow(2,source.Count); i++)
{
List<List<String>> tempL = new List<List<String>>(); Int32 tt = (Int32)Math.Pow(2, source.Count);
for (Int32 j = 1;; j *= 2)
{
if (tt == 1)
break; if ((i & j) == j)
{
if (j == 1)
{
tempL.Add(source[0]);
}
else
{
tempL.Add(source[(Int32)Math.Sqrt(j)]);
}
}
tt = tt >> 1;
}
if (tempL.Count == 1)
{
result.AddRange(tempL[0]);
}
else
{
for (Int32 k = 1; k < tempL.Count; k++)
{
tempL[k] = GetMerge(tempL[k - 1], tempL[k]);
result.AddRange(tempL[k]);
}
}
} foreach (String s in result)
Console.WriteLine(s);
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5"); string[][] strLstArr = new string[lista.Count][];
for (int i = 0; i < lista.Count; i++)
{
strLstArr[i] = new string[] { lista[i], listb[i], listc[i], listd[i] };
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int j = 0; j < strLstArr[i].Length; j++)
{
Response.Write(strLstArr[i][j]);//如果不需要打印单元素的组合,将此句注释掉
Response.Write("<br />");
dic.Add(strLstArr[i][j], j);
}
GetString(dic, strLstArr[i]);
Response.Write("<br />");
}private void GetString(Dictionary<string, int> dd, string[] m_Data)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < m_Data.Length; i++)
{
Response.Write(kv.Key + m_Data[i]);
Response.Write("<br />");
dic.Add(kv.Key + m_Data[i], i);
}
}
if (dic.Count > 0) GetString(dic, m_Data);
}
不知LZ要的是不是这样?
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5"); string[][] strLstArr = new string[lista.Count][];
for (int i = 0; i < lista.Count; i++)
{
strLstArr[i] = new string[] { lista[i], listb[i], listc[i], listd[i] };
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int j = 0; j < strLstArr[i].Length; j++)
{
Response.Write(strLstArr[i][j]);//如果不需要打印单元素的组合,将此句注释掉
Response.Write("<br />");
dic.Add(strLstArr[i][j], j);
}
GetString(dic, strLstArr[i]);
Response.Write("<br />");
}private void GetString(Dictionary<string, int> dd, string[] m_Data)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < m_Data.Length; i++)
{
Response.Write(kv.Key + m_Data[i]);
Response.Write("<br />");
dic.Add(kv.Key + m_Data[i], i);
}
}
if (dic.Count > 0) GetString(dic, m_Data);
}
结果是:
测试数据A1
测试数据B1
测试数据C1
测试数据D1
测试数据A1测试数据B1
测试数据A1测试数据C1
测试数据A1测试数据D1
测试数据B1测试数据C1
测试数据B1测试数据D1
测试数据C1测试数据D1
测试数据A1测试数据B1测试数据C1
测试数据A1测试数据B1测试数据D1
测试数据A1测试数据C1测试数据D1
测试数据B1测试数据C1测试数据D1
测试数据A1测试数据B1测试数据C1测试数据D1测试数据A2
测试数据B2
测试数据C2
测试数据D2
测试数据A2测试数据B2
测试数据A2测试数据C2
测试数据A2测试数据D2
测试数据B2测试数据C2
测试数据B2测试数据D2
测试数据C2测试数据D2
测试数据A2测试数据B2测试数据C2
测试数据A2测试数据B2测试数据D2
测试数据A2测试数据C2测试数据D2
测试数据B2测试数据C2测试数据D2
测试数据A2测试数据B2测试数据C2测试数据D2测试数据A3
测试数据B3
测试数据C3
测试数据D3
测试数据A3测试数据B3
测试数据A3测试数据C3
测试数据A3测试数据D3
测试数据B3测试数据C3
测试数据B3测试数据D3
测试数据C3测试数据D3
测试数据A3测试数据B3测试数据C3
测试数据A3测试数据B3测试数据D3
测试数据A3测试数据C3测试数据D3
测试数据B3测试数据C3测试数据D3
测试数据A3测试数据B3测试数据C3测试数据D3测试数据A4
测试数据B4
测试数据C4
测试数据D4
测试数据A4测试数据B4
测试数据A4测试数据C4
测试数据A4测试数据D4
测试数据B4测试数据C4
测试数据B4测试数据D4
测试数据C4测试数据D4
测试数据A4测试数据B4测试数据C4
测试数据A4测试数据B4测试数据D4
测试数据A4测试数据C4测试数据D4
测试数据B4测试数据C4测试数据D4
测试数据A4测试数据B4测试数据C4测试数据D4测试数据A5
测试数据B5
测试数据C5
测试数据D5
测试数据A5测试数据B5
测试数据A5测试数据C5
测试数据A5测试数据D5
测试数据B5测试数据C5
测试数据B5测试数据D5
测试数据C5测试数据D5
测试数据A5测试数据B5测试数据C5
测试数据A5测试数据B5测试数据D5
测试数据A5测试数据C5测试数据D5
测试数据B5测试数据C5测试数据D5
测试数据A5测试数据B5测试数据C5测试数据D5
要的是不是这样?
using System.Collections.Generic;class test
{
static List<string>[] lists;
static void initLists()//初始化数据
{
List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5");
lists = new List<string>[] { lista, listb, listc, listd };
}
static Random rnd = new Random();
static IList<string> getDatas(bool[] masks)
{
List<string> ret = new List<string>(lists.Length);
for (int i = 0; i < lists.Length; ++i)
{
if (masks[i])
{
ret.Add(lists[i][rnd.Next(0, lists[i].Count)]);
}
}
return ret;
}
class ArrangementContext
{
public int m,n;
public int[] result;//结果集
public bool[] flags;//使用标记 public void reset(int idx)
{
flags[idx] = false;
}
public void set(int idx)
{
flags[idx] = true;
}
public ArrangementContext(int n, int m)
{
result = new int[m];
flags = new bool[n];
this.m = m;
this.n = n;
}
public int this[int idx]
{
get { return result[idx]; }
set { result[idx] = value; }
}
} static void A(ArrangementContext ac,int cursor, Action<int[]> handler)//递归求排列
{
if (cursor == ac.m)
{
handler(ac.result);
return;
}
for (int i = 0; i < ac.flags.Length; ++i)
{
if (!ac.flags[i])
{
ac.set(i);
ac.result[cursor] = i;
A(ac, cursor + 1, handler);
ac.reset(i);
}
}
}
static void A(int n, int m, Action<int[]> handler)
{
ArrangementContext ac = new ArrangementContext(n, m);
A(ac,0, handler);
}//求排列
static void Main(string[] argv)
{
initLists(); Console.WriteLine("随机抽取数据:\n");
var datas = getDatas(new bool[] { false, true, true, true });//false位对应的list将不取值
foreach (var data in datas)
Console.WriteLine(data); Console.WriteLine("\n求排列数据:\n"); int count = 0;
A(datas.Count, datas.Count, (r) =>//求全排列
{
for (int i = 0; i < r.Length - 1; ++i)
Console.Write("{0},",datas[r[i]]);
Console.WriteLine(datas[r[r.Length - 1]]);
++count;
});
Console.Write("\n共{0}种排列", count);
}
}结果:随机抽取数据:测试数据B3
测试数据C3
测试数据D2求排列数据:测试数据B3,测试数据C3,测试数据D2
测试数据B3,测试数据D2,测试数据C3
测试数据C3,测试数据B3,测试数据D2
测试数据C3,测试数据D2,测试数据B3
测试数据D2,测试数据B3,测试数据C3
测试数据D2,测试数据C3,测试数据B3共6种排列
假设随机抽取了测试数据B3
测试数据C3
测试数据D2排列出来的数据测试数据B3测试数据B3测试数据D2
这个也要算一个组合哦。我可能取其中一个,或者多个。-,-不需要随机抽取,要全部的。
using System.Collections.Generic;class test
{
static List<string>[] lists;
static void initLists()//初始化数据
{
List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5");
lists = new List<string>[] { lista, listb, listc, listd };
}
static Random rnd = new Random();
static IList<string> getDatas(bool[] masks)
{
List<string> ret = new List<string>(lists.Length);
for (int i = 0; i < lists.Length; ++i)
{
if (masks[i])
{
ret.Add(lists[i][rnd.Next(0, lists[i].Count)]);
}
}
return ret;
}
class ArrangementContext
{
public int m,n;
public int[] result;//结果集
public bool[] flags;//使用标记 public void reset(int idx)
{
flags[idx] = false;
}
public void set(int idx)
{
flags[idx] = true;
}
public ArrangementContext(int n, int m)
{
result = new int[m];
flags = new bool[n];
this.m = m;
this.n = n;
}
public int this[int idx]
{
get { return result[idx]; }
set { result[idx] = value; }
}
} static void A(ArrangementContext ac,int cursor, Action<int[]> handler)//递归求排列
{
if (cursor == ac.m)
{
handler(ac.result);
return;
}
for (int i = 0; i < ac.flags.Length; ++i)
{
if (!ac.flags[i])
{
ac.set(i);
ac.result[cursor] = i;
A(ac, cursor + 1, handler);
ac.reset(i);
}
}
}
static void A(int n, int m, Action<int[]> handler)
{
ArrangementContext ac = new ArrangementContext(n, m);
A(ac,0, handler);
}//求排列
static void Main(string[] argv)
{
initLists(); Console.WriteLine("随机抽取数据:\n");
var datas = getDatas(new bool[] { true, true, true, true });//false位对应的list将不取值
foreach (var data in datas)
Console.WriteLine(data); Console.WriteLine("\n求排列数据:\n"); int count = 0;
for (int m = 0; m < datas.Count; ++m)
{
A(datas.Count, m+1, (r) =>
{
for (int i = 0; i < r.Length - 1; ++i)
Console.Write("{0},", datas[r[i]]);
Console.WriteLine(datas[r[r.Length - 1]]);
++count;
});
}
Console.Write("\n共{0}种排列", count);
}
}貌似lz要的是另一种结果:
随机抽取数据:测试数据A4
测试数据B1
测试数据C5
测试数据D4求排列数据:测试数据A4
测试数据B1
测试数据C5
测试数据D4
测试数据A4,测试数据B1
测试数据A4,测试数据C5
测试数据A4,测试数据D4
测试数据B1,测试数据A4
测试数据B1,测试数据C5
测试数据B1,测试数据D4
测试数据C5,测试数据A4
测试数据C5,测试数据B1
测试数据C5,测试数据D4
测试数据D4,测试数据A4
测试数据D4,测试数据B1
测试数据D4,测试数据C5
测试数据A4,测试数据B1,测试数据C5
测试数据A4,测试数据B1,测试数据D4
测试数据A4,测试数据C5,测试数据B1
测试数据A4,测试数据C5,测试数据D4
测试数据A4,测试数据D4,测试数据B1
测试数据A4,测试数据D4,测试数据C5
测试数据B1,测试数据A4,测试数据C5
测试数据B1,测试数据A4,测试数据D4
测试数据B1,测试数据C5,测试数据A4
测试数据B1,测试数据C5,测试数据D4
测试数据B1,测试数据D4,测试数据A4
测试数据B1,测试数据D4,测试数据C5
测试数据C5,测试数据A4,测试数据B1
测试数据C5,测试数据A4,测试数据D4
测试数据C5,测试数据B1,测试数据A4
测试数据C5,测试数据B1,测试数据D4
测试数据C5,测试数据D4,测试数据A4
测试数据C5,测试数据D4,测试数据B1
测试数据D4,测试数据A4,测试数据B1
测试数据D4,测试数据A4,测试数据C5
测试数据D4,测试数据B1,测试数据A4
测试数据D4,测试数据B1,测试数据C5
测试数据D4,测试数据C5,测试数据A4
测试数据D4,测试数据C5,测试数据B1
测试数据A4,测试数据B1,测试数据C5,测试数据D4
测试数据A4,测试数据B1,测试数据D4,测试数据C5
测试数据A4,测试数据C5,测试数据B1,测试数据D4
测试数据A4,测试数据C5,测试数据D4,测试数据B1
测试数据A4,测试数据D4,测试数据B1,测试数据C5
测试数据A4,测试数据D4,测试数据C5,测试数据B1
测试数据B1,测试数据A4,测试数据C5,测试数据D4
测试数据B1,测试数据A4,测试数据D4,测试数据C5
测试数据B1,测试数据C5,测试数据A4,测试数据D4
测试数据B1,测试数据C5,测试数据D4,测试数据A4
测试数据B1,测试数据D4,测试数据A4,测试数据C5
测试数据B1,测试数据D4,测试数据C5,测试数据A4
测试数据C5,测试数据A4,测试数据B1,测试数据D4
测试数据C5,测试数据A4,测试数据D4,测试数据B1
测试数据C5,测试数据B1,测试数据A4,测试数据D4
测试数据C5,测试数据B1,测试数据D4,测试数据A4
测试数据C5,测试数据D4,测试数据A4,测试数据B1
测试数据C5,测试数据D4,测试数据B1,测试数据A4
测试数据D4,测试数据A4,测试数据B1,测试数据C5
测试数据D4,测试数据A4,测试数据C5,测试数据B1
测试数据D4,测试数据B1,测试数据A4,测试数据C5
测试数据D4,测试数据B1,测试数据C5,测试数据A4
测试数据D4,测试数据C5,测试数据A4,测试数据B1
测试数据D4,测试数据C5,测试数据B1,测试数据A4共64种排列
4个list总共有16个元素,单16个元素的全排列就有16! = 20922789888000种可能
按你的要求的所有排列的总和16
Σ i!
i=1换台大型机去做比较有可能....
但那也是一个不小的数字.
4
(5^5 - 1) * Σ i!
i=1 其实不管怎么输出排列算法原理都一样.
lz自己看下应该能总结出规律.
重写ing...
peter兄,没无视你,我在测试你的代码勒。刚和别人在讨论,不好意思啊。。
-,-里面好多重复的,你看下。
测试数据C1,测试数据C2
测试数据C1,测试数据C3
测试数据C1,测试数据C4
测试数据C1,测试数据C5
测试数据C2,测试数据C1
测试数据C2,测试数据C2
测试数据C2,测试数据C3
测试数据C2,测试数据C4
测试数据C2,测试数据C5
测试数据C3,测试数据C1
测试数据C3,测试数据C2
测试数据C3,测试数据C3
测试数据C3,测试数据C4
测试数据C3,测试数据C5
测试数据C4,测试数据C1
测试数据C4,测试数据C2
测试数据C4,测试数据C3
测试数据C4,测试数据C4
测试数据C4,测试数据C5
测试数据C5,测试数据C1
测试数据C5,测试数据C2
测试数据C5,测试数据C3
测试数据C5,测试数据C4
测试数据C5,测试数据C5
测试数据A1,测试数据C1
测试数据A1,测试数据C2
测试数据A1,测试数据C3
测试数据A1,测试数据C4
测试数据A1,测试数据C5
测试数据A2,测试数据C1
测试数据A2,测试数据C2
测试数据A2,测试数据C3
测试数据A2,测试数据C4
测试数据A2,测试数据C5
测试数据A3,测试数据C1
测试数据A3,测试数据C2
测试数据A3,测试数据C3
测试数据A3,测试数据C4
测试数据A3,测试数据C5
测试数据A4,测试数据C1
测试数据A4,测试数据C2
测试数据A4,测试数据C3
测试数据A4,测试数据C4
测试数据A4,测试数据C5
测试数据A5,测试数据C1
测试数据A5,测试数据C2
测试数据A5,测试数据C3
测试数据A5,测试数据C4
测试数据A5,测试数据C5
测试数据A1,测试数据C1,测试数据C1
测试数据A1,测试数据C1,测试数据C2
测试数据A1,测试数据C1,测试数据C3
测试数据A1,测试数据C1,测试数据C4
测试数据A1,测试数据C1,测试数据C5
测试数据A1,测试数据C2,测试数据C1
测试数据A1,测试数据C2,测试数据C2
测试数据A1,测试数据C2,测试数据C3
测试数据A1,测试数据C2,测试数据C4
测试数据A1,测试数据C2,测试数据C5
测试数据A1,测试数据C3,测试数据C1
测试数据A1,测试数据C3,测试数据C2
测试数据A1,测试数据C3,测试数据C3
测试数据A1,测试数据C3,测试数据C4
测试数据A1,测试数据C3,测试数据C5
测试数据A1,测试数据C4,测试数据C1
测试数据A1,测试数据C4,测试数据C2
测试数据A1,测试数据C4,测试数据C3
测试数据A1,测试数据C4,测试数据C4
测试数据A1,测试数据C4,测试数据C5
测试数据A1,测试数据C5,测试数据C1
测试数据A1,测试数据C5,测试数据C2
测试数据A1,测试数据C5,测试数据C3
-,-这儿数据成这样了。
protected void Page_Load(object sender, EventArgs e)
{
List<string> list1 = new List<string>();
list1.Add("测试数据A1");
list1.Add("测试数据A2");
List<string> list2 = new List<string>();
list2.Add("测试数据B1");
list2.Add("测试数据B2");
List<string> list3 = new List<string>();
list3.Add("测试数据C1");
list3.Add("测试数据C2"); int n = 3;
List<string> mid = new List<string>();
List<string> all = new List<string>();
list1.Add("");
for (int i = 0; i < list1.Count; i++)
{
mid.AddRange(retList(list1[i], list2));
} all.Clear();
all.AddRange(mid);
mid.Clear();
for (int i = 0; i < all.Count; i++)
{
mid.AddRange(retList(all[i], list3));
}
all.Clear();
all.AddRange(mid);
mid.Clear();
Response.Write("<br />");
for (int i = 0; i < all.Count; i++)
{
if (all[i] == "")
continue;
Response.Write(all[i] + "<br />");
}
}
public IList<string> retList(string a, List<string> b)
{
List<string> c = new List<string>();
c.Add(a);
foreach (string s in b)
{
c.Add(a + " " + s);
}
return c;
}我也要来凑热闹 不用考虑我的方法
把第一个list的每个数据看做第一层树根.
第二个list的每个元素看做每个根的子节点.
第三个list的每个元素看做第二个list每个元素的子节点.以此类推,你要的结果就是遍历这些树时走过的路径集using System;
using System.Collections.Generic;class test
{
static List<string>[] lists;
static void initLists()//初始化数据
{
List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5");
lists = new List<string>[] { lista, listb, listc, listd };
}
static void ViewTree(string[] result ,int lv,Action<string[],int> handler)
{
if (lv == lists.Length)
return;
for (int i = 0; i < lists[lv].Count; ++i)
{
result[lv] = lists[lv][i];
handler(result, lv);
ViewTree(result, lv + 1, handler);
}
}
static void Main(string[] argv)
{
initLists();
Console.WriteLine("\n求组合数据:\n");
int count = 0;
string[] result = new string[lists.Length];
ViewTree (result,0,(r,lv)=>{
Console.WriteLine (String.Join(",", r, 0, lv + 1));
count++;
});
Console.WriteLine("\n共{0}种组合", count);
}
}
tempL.Add(source[(Int32)Math.Sqrt(j)]);
这错了
应该是求以2为底,j的对数
tempL.Add(source[(Int32)Math.Log(j,2)]);这次你试下
//sorry for my mistake
-,-好像可以勒。如果是3个list,每个list有5个数据得到的组合是240个,总数是对的么?
public static List<String> GetMerge(List<String> l1, List<String> l2)
{
List<String> list = new List<String>(); for (Int32 i = 0; i < l1.Count; i++)
{
for (Int32 j = 0; j < l2.Count; j++)
{
list.Add(l1[i] + "," + l2[j]);
}
}
return list;
} static void Main(string[] args)
{ List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5"); List<List<String>> source = new List<List<String>>();
source.Add(lista);
source.Add(listb);
source.Add(listc);
source.Add(listd); List<String> result = new List<String>(); for (Int32 i = 1; i < Math.Pow(2, source.Count); i++)
{
List<List<String>> tempL = new List<List<String>>(); Int32 tt = (Int32)Math.Pow(2, source.Count);
for (Int32 j = 1; ; j *= 2)
{
if (tt == 1)
break; if ((i & j) == j)
{
if (j == 1)
{
tempL.Add(source[0]);
}
else
{
tempL.Add(source[(Int32)Math.Log(j,2)]);
}
}
tt = tt >> 1;
}
if (tempL.Count == 1)
{
result.AddRange(tempL[0]);
}
else
{
for (Int32 k = 1; k < tempL.Count; k++)
{
tempL[k] = GetMerge(tempL[k - 1], tempL[k]);
result.AddRange(tempL[k]);
}
} }
result = result.Distinct<String>().ToList<String>(); foreach (String s in result)
Console.WriteLine(s); Console.WriteLine(result.Count);
}
protected void Page_Load(object sender, EventArgs e)
{
List<string> list1 = new List<string>();
list1.Add("测试数据A1");
list1.Add("测试数据A2");
List<string> list2 = new List<string>();
list2.Add("测试数据B1");
list2.Add("测试数据B2");
List<string> list3 = new List<string>();
list3.Add("测试数据C1");
list3.Add("测试数据C2");
List<List<string>> allList = new List<List<string>>();
allList.Add(list1);
allList.Add(list2);
allList.Add(list3);
int j = 0;
List<string> mid = new List<string>();
List<string> all = new List<string>();
list1.Add("");
all = list1;
while (j < allList.Count - 1)
{
for (int i = 0; i < all.Count; i++)
{
mid.AddRange(retList(all[i], allList[j + 1]));
}
all.Clear();
all.AddRange(mid);
mid.Clear();
j++; } for (int i = 0; i < all.Count; i++)
{
if (all[i] == "")
continue;
Response.Write(all[i] + "<br />");
} }
public IList<string> retList(string a, List<string> b)
{
List<string> c = new List<string>(); foreach (string s in b)
{
c.Add(a + " " + s);
}
c.Add(a);
return c;
}
这样写没有重复的 不过性能方面我不敢保证
测试数据A1 测试数据B1 测试数据C2
测试数据A1 测试数据B1
测试数据A1 测试数据B2 测试数据C1
测试数据A1 测试数据B2 测试数据C2
测试数据A1 测试数据B2
测试数据A1 测试数据C1
测试数据A1 测试数据C2
测试数据A1
测试数据A2 测试数据B1 测试数据C1
测试数据A2 测试数据B1 测试数据C2
测试数据A2 测试数据B1
测试数据A2 测试数据B2 测试数据C1
测试数据A2 测试数据B2 测试数据C2
测试数据A2 测试数据B2
测试数据A2 测试数据C1
测试数据A2 测试数据C2
测试数据A2
测试数据B1 测试数据C1
测试数据B1 测试数据C2
测试数据B1
测试数据B2 测试数据C1
测试数据B2 测试数据C2
测试数据B2
测试数据C1
测试数据C2
结果
你的测试结果是:
测试数据A1 测试数据B1 测试数据C1
测试数据A1 测试数据B1 测试数据C2
测试数据A1 测试数据B1
测试数据A1 测试数据B2 测试数据C1
测试数据A1 测试数据B2 测试数据C2
测试数据A1 测试数据B2
测试数据A1 测试数据C1
测试数据A1 测试数据C2
测试数据A1
测试数据A2 测试数据B1 测试数据C1
测试数据A2 测试数据B1 测试数据C2
测试数据A2 测试数据B1
测试数据A2 测试数据B2 测试数据C1
测试数据A2 测试数据B2 测试数据C2
测试数据A2 测试数据B2
测试数据A2 测试数据C1
测试数据A2 测试数据C2
测试数据A2
测试数据B1 测试数据C1
测试数据B1 测试数据C2
测试数据B1
测试数据B2 测试数据C1
测试数据B2 测试数据C2
测试数据B2
测试数据C1
测试数据C2组合数据有丢失-,-
{
List<String> list = new List<String>(); for (Int32 i = 0; i < l1.Count; i++)
{
for (Int32 j = 0; j < l2.Count; j++)
{
list.Add(l1[i] + "," + l2[j]);
}
}
return list;
} static void Main(string[] args)
{ List<string> lista = new List<string>();
lista.Add("测试数据A1");
//lista.Add("测试数据A2");
//lista.Add("测试数据A3");
//lista.Add("测试数据A4");
//lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
//listb.Add("测试数据B2");
//listb.Add("测试数据B3");
//listb.Add("测试数据B4");
//listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
//listc.Add("测试数据C2");
//listc.Add("测试数据C3");
//listc.Add("测试数据C4");
//listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
//listd.Add("测试数据D2");
//listd.Add("测试数据D3");
//listd.Add("测试数据D4");
//listd.Add("测试数据D5"); List<List<String>> source = new List<List<String>>();
source.Add(lista);
source.Add(listb);
source.Add(listc);
source.Add(listd); List<String> result = new List<String>(); for (Int32 i = 1; i < Math.Pow(2, source.Count); i++)
{
List<List<String>> tempL = new List<List<String>>(); Int32 tt = (Int32)Math.Pow(2, source.Count);
for (Int32 j = 1; ; j *= 2)
{
if (tt == 1)
break; if ((i & j) == j)
{
if (j == 1)
{
tempL.Add(source[0]);
}
else
{
tempL.Add(source[(Int32)Math.Log(j,2)]);
}
}
tt = tt >> 1;
}
if (tempL.Count == 1)
{
result.AddRange(tempL[0]);
}
else
{
Int32 k = 1;
for ( ; k < tempL.Count; k++)
{
tempL[k] = GetMerge(tempL[k - 1], tempL[k]);
}
result.AddRange(tempL[k -1]);
} } foreach (String s in result)
Console.WriteLine(s);
{
List<string> lista = new List<string>();
lista.Add("测试数据A1");
lista.Add("测试数据A2");
lista.Add("测试数据A3");
lista.Add("测试数据A4");
lista.Add("测试数据A5");
List<string> listb = new List<string>();
listb.Add("测试数据B1");
listb.Add("测试数据B2");
listb.Add("测试数据B3");
listb.Add("测试数据B4");
listb.Add("测试数据B5");
List<string> listc = new List<string>();
listc.Add("测试数据C1");
listc.Add("测试数据C2");
listc.Add("测试数据C3");
listc.Add("测试数据C4");
listc.Add("测试数据C5");
List<string> listd = new List<string>();
listd.Add("测试数据D1");
listd.Add("测试数据D2");
listd.Add("测试数据D3");
listd.Add("测试数据D4");
listd.Add("测试数据D5");
int A = lista.Count;
int B = listb.Count;
int C = listc.Count;
int D = listd.Count;
int a, b, c, d;
for (a = 0; a < A; ++a)
for(b=0;b<B;++b)
for(c=0;c<D;++c)
for (d = 0; d < 5; ++d)
{
Console.WriteLine("{0} {1} {2} `{3}", lista[a] , listb[b] , listc[c] , listd[d]);
}
}
测试数据A1 测试数据B1 测试数据C1 `测试数据D1
测试数据A1 测试数据B1 测试数据C1 `测试数据D2
测试数据A1 测试数据B1 测试数据C1 `测试数据D3
测试数据A1 测试数据B1 测试数据C1 `测试数据D4
测试数据A1 测试数据B1 测试数据C1 `测试数据D5
测试数据A1 测试数据B1 测试数据C2 `测试数据D1
测试数据A1 测试数据B1 测试数据C2 `测试数据D2
测试数据A1 测试数据B1 测试数据C2 `测试数据D3
.............................
测试数据A1 测试数据B5 测试数据C4 `测试数据D3
测试数据A1 测试数据B5 测试数据C4 `测试数据D4
测试数据A1 测试数据B5 测试数据C4 `测试数据D5
测试数据A1 测试数据B5 测试数据C5 `测试数据D1
测试数据A1 测试数据B5 测试数据C5 `测试数据D2
测试数据A1 测试数据B5 测试数据C5 `测试数据D3
测试数据A1 测试数据B5 测试数据C5 `测试数据D4
测试数据A1 测试数据B5 测试数据C5 `测试数据D5
测试数据A2 测试数据B1 测试数据C1 `测试数据D1
测试数据A2 测试数据B1 测试数据C1 `测试数据D2
测试数据A2 测试数据B1 测试数据C1 `测试数据D3
测试数据A2 测试数据B1 测试数据C1 `测试数据D4
测试数据A2 测试数据B1 测试数据C1 `测试数据D5
测试数据A2 测试数据B1 测试数据C2 `测试数据D1
测试数据A2 测试数据B1 测试数据C2 `测试数据D2
测试数据A2 测试数据B1 测试数据C2 `测试数据D3
测试数据A2 测试数据B1 测试数据C2 `测试数据D4
测试数据A2 测试数据B1 测试数据C2 `测试数据D5
测试数据A2 测试数据B1 测试数据C3 `测试数据D1
测试数据A2 测试数据B1 测试数据C3 `测试数据D2
测试数据A2 测试数据B1 测试数据C3 `测试数据D3
.....................
测试数据A5 测试数据B5 测试数据C3 `测试数据D2
测试数据A5 测试数据B5 测试数据C3 `测试数据D3
测试数据A5 测试数据B5 测试数据C3 `测试数据D4
测试数据A5 测试数据B5 测试数据C3 `测试数据D5
测试数据A5 测试数据B5 测试数据C4 `测试数据D1
测试数据A5 测试数据B5 测试数据C4 `测试数据D2
测试数据A5 测试数据B5 测试数据C4 `测试数据D3
测试数据A5 测试数据B5 测试数据C4 `测试数据D4
测试数据A5 测试数据B5 测试数据C4 `测试数据D5
测试数据A5 测试数据B5 测试数据C5 `测试数据D1
测试数据A5 测试数据B5 测试数据C5 `测试数据D2
测试数据A5 测试数据B5 测试数据C5 `测试数据D3
测试数据A5 测试数据B5 测试数据C5 `测试数据D4
测试数据A5 测试数据B5 测试数据C5 `测试数据D5