| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 467 人关注过本帖
标题:求助,关于一个算法问题
只看楼主 加入收藏
xiaochun662003
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2006-7-8
收藏
 问题点数:0 回复次数:2 
求助,关于一个算法问题
有数组CC()值为::
pid=2
pid=124
pid=2
pid=125
pid=2
pid=126
pid=2
pid=125
pid=134

如何删除数组中的重复元素,并生成查询格式如:
pid=2 or pid=124 or pid=125 or pid=126 or pid=134

[此贴子已经被作者于2006-7-26 13:28:55编辑过]

搜索更多相关主题的帖子: 算法 
2006-07-26 12:00
shuijingling
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-7-19
收藏
得分:0 

class Program
{
static void Main(string[] args)
{
int[] cc ={2, 124, 2, 125, 2, 126, 125, 134 };
for (int i = 0; i < cc.Length; i++)
Console.Write(cc[i] + "\t");
Console.WriteLine();
int n = 0;
for (int i = 0; i < cc.Length; i++) //从第一个数开始
{
if (cc[i] == 0) break;
for (int j = i + 1; j < cc.Length; j++) // 跟他后面的数比较
{
if (cc[i] == cc[j]) // 如果相等的话
{
for (int k = j; k < cc.Length - 1; k++) //后面的数就向前移一位,覆盖掉前面的数据
cc[k] = cc[k + 1];
n++;
cc[cc.Length - n] = 0; //每次覆盖掉一个数据,从后往前值零
for (int s = 0; s < cc.Length; s++)
Console.Write(cc[s] + "\t");
Console.WriteLine();
}

}
}
for (int i = 0; i < cc.Length; i++)
Console.Write(cc[i] + "\t");
Console.Read();
}
}


================================================================================================
打印结果:
2 124 2 125 2 126 125 134
2 124 125 2 126 125 134 0
2 124 125 126 125 134 0 0
2 124 125 126 134 0 0 0
2 124 125 126 134 0 0 0

这是用最笨的方法写的,可能时间和效率上都不行,我也是初学者,有更好方法的请指教!

[此贴子已经被作者于2006-7-26 14:57:34编辑过]

2006-07-26 14:55
xiaochun662003
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2006-7-8
收藏
得分:0 
问题解决

先把CC数组按" or "串连成字符串 itemtj
itemtj="pid=2 or pid=124 or pid=125 or pid=126 or pid=134"
再执行以下代码:
cc=Split(trim(itemtj)," or ")
'-----------修剪条件-------------
for i=0 to ubound(cc)
if instr(itemtj,cc(i))=1 then
itemtj=replace(itemtj,"or "&trim(cc(i)),"")
else
itemtj=replace(itemtj,"or "&trim(cc(i)),"")&" or "&cc(i)
end if

next

即得到:
pid=2 or pid=124 or pid=125 or pid=126 or pid=134

希望大家有不同的算法.

[此贴子已经被作者于2006-7-27 11:14:14编辑过]

2006-07-27 10:48
快速回复:求助,关于一个算法问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011318 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved