找回密码
 欢迎注册
查看: 13865|回复: 2

[求助] 用算法如何实现类似数据库中的笛卡尔积?

[复制链接]
发表于 2008-8-14 10:59:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
有n类事件,每一类事件又有若干个例子。如A(1,2,3),B(1,2,3),C(1,2),D(1,2),现在要对各个事件的例子组合进行操作,请问如何对这些组合进行存储,也就是采用何种数据结构比较合理?我用例子说明组合的意思: 每个事件单独表示为下面的表: table1 table2 table3 table4 A B C D ------- ------- ------ ----- 1 1 1 1 2 2 2 2 3 3 当事件之间两两组合时,就得到以下六个表: table5 table6 table7 table8 table9 table10 A B A C A D B C B D C D ------- ------- ----- ------- ------ ------- 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 1 2 1 2 1 2 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 3 1 3 1 3 1 3 1 2 3 3 2 3 2 3 2 3 2 3 1 3 2 3 3 当事件每三个三个组合时,得到下面的表: table11 table12 table13 table14 A B C A B D A C D B C D --------- -------- -------- -------- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 2 1 1 2 1 2 1 1 2 1 1 2 1 1 2 1 1 2 2 1 2 2 1 2 2 1 2 2 1 3 1 1 3 1 2 1 1 2 1 1 1 3 2 1 3 2 2 1 2 2 1 2 . . . . . . . . . . . . . . . . 四个事件之间组合得到下面的表: table15 A B C D ------------------ 1 1 1 1 1 1 1 2 1 1 2 1 1 1 2 2 1 2 1 1 1 2 1 2 . . . . 我要如何对这些表进行统一的表示呢?也就是采用何种数据结构比较合理?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-8-19 17:27:47 | 显示全部楼层
已解决,在csdn 上找到了正解,跟大家分享一下! 以4类事件为例: string[][] a = new string[4][]; a[0] = new string[] { "king", "of", "the", "world" }; a[1] = new string[] { "cs", "app" }; a[2] = new string[] { "good", "cool", "dev" }; a[3] = new string[] { "king", "of", "the"}; System.Collections.ArrayList arrlist = new System.Collections.ArrayList(); int[] index = new int[a.Length]; while (index[0] < a[0].Length) { string temp = ""; for (int i = 0; i < index.Length; i++) { temp += a[i][index[i]] + "&"; } arrlist.Add(temp.Substring(0, temp.Length - 1)); index[index.Length - 1]++; for (int i = index.Length - 1; i > 0; i--) { if (index[i] >= a[i].Length) { index[i] = 0; index[i - 1]++; } } } Array b = arrlist.ToArray(typeof(string));
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-8-19 17:33:56 | 显示全部楼层
测试结果 king&cs&good&king king&cs&good&of king&cs&good&the king&cs&cool&king king&cs&cool&of king&cs&cool&the king&cs&dev&king king&cs&dev&of king&cs&dev&the king&app&good&kin king&app&good&of king&app&good&the king&app&cool&kin king&app&cool&of king&app&cool&the king&app&dev&king king&app&dev&of king&app&dev&the of&cs&good&king of&cs&good&of of&cs&good&the of&cs&cool&king of&cs&cool&of of&cs&cool&the of&cs&dev&king of&cs&dev&of of&cs&dev&the of&app&good&king of&app&good&of of&app&good&the of&app&cool&king of&app&cool&of of&app&cool&the of&app&dev&king of&app&dev&of of&app&dev&the the&cs&good&king the&cs&good&of the&cs&good&the the&cs&cool&king the&cs&cool&of the&cs&cool&the the&cs&dev&king the&cs&dev&of the&cs&dev&the the&app&good&king the&app&good&of the&app&good&the the&app&cool&king the&app&cool&of the&app&cool&the the&app&dev&king the&app&dev&of the&app&dev&the world&cs&good&kin world&cs&good&of world&cs&good&the world&cs&cool&kin world&cs&cool&of world&cs&cool&the world&cs&dev&king world&cs&dev&of world&cs&dev&the world&app&good&ki world&app&good&of world&app&good&th world&app&cool&ki world&app&cool&of world&app&cool&th world&app&dev&kin world&app&dev&of world&app&dev&the
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-11-22 00:41 , Processed in 0.029113 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表