这样的数字处理怎么写,需要考虑速度问题(200)

E

edren

Unregistered / Unconfirmed
GUEST, unregistred user!
一个LISTBOX1的内容是00001到99999所有5位的数字,比如0000100003000040000700008000090001000012等等....按button1后快速找出listbox1中不存在的数字,然后显示在listbox2里,比如0000200005000060001100013等等....
 
只能从00001到99999逐个查找,如果在listbox1中没有找到则填加到listbox2中,为了提高速度可使用多线程每个线程检测一段数字。如线程一检测00001到00999,线程二检测01000到01999...线程N检测98999到99999。
 
看看哈希表处理,你就会明白了。简单的例子,金山词霸怎么能从上百万条单词中瞬间找到你要的单词?
 
用了40000条数据测试下列代码,耗时1s左右,不知道是否满足你的要求。前提是Listbox1中数据是按顺序显示的,且必须都是数字。procedure TForm1.Button2Click(Sender: TObject);const MAXVALUE = 99999;var Index, iRow: Integer
iTmp: Integer
procedure DoAdd
begin Listbox2.Items.Add(inttostr(Index))
end;begin iRow := 1
Index := 1
Listbox2.Items.BeginUpdate
try while (Index <= MAXVALUE) and (iRow <= Listbox1.Count) do begin iTmp := StrToInt(Listbox1.Items[iRow - 1])
if iTmp <> Index then begin DoAdd()
end else Inc(iRow)
Inc(Index)
end
while (Index <= MAXVALUE) do begin DoAdd
Inc(Index)
end
finally Listbox2.Items.EndUpdate
end;end;
 
其实也没那么讲究,只要不太慢就好
 
你在SQL数据库里建一张表,内容就是完整的1-99999,然后把Listbox1里的数据导到另一张表里后用类似于 Select s.id From sourcetable s, listboxtable l Where l.id <> s.id的语句查找试试。
 
小软件,不用数据库,能小则小,能简单则简单
 
顺序查找 I := 0 to 9999 找 listbox1.items[J] 如果 > I ,而且没有找到放 listbox2否则继续下一个I
 
来自:edren, 时间:2009-4-9 8:56:57, ID:3952968其实也没那么讲究,只要不太慢就好 来自:edren, 时间:2009-4-9 9:08:14, ID:3952974小软件,不用数据库,能小则小,能简单则简单 -----------------------不知道我上面的代码是否符合你的要求?或者能否把你的要求再说具体点?
 
必须是5位数字,而且您那代码没效果。我刚才测试了一下,不过还是谢谢啊
 
我刚才在LISTBOX1里装入了00000到99999的所有数字后,删除了几个来试的,好象您那代码没有用,LISTBOX1存在的一样会加到LISTBOX2去
 
不会吧,我测试可以的。代码全部给你:unit:--------------unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, TeEngine, Series, TeeProcs, Chart, AppEvnts, DB, DBClient, TeeFunci, typinfo, ADODB;type TForm1 = class(TForm) ListBox1: TListBox
ListBox2: TListBox
Button2: TButton
Button3: TButton
procedure Button2Click(Sender: TObject)
procedure Button3Click(Sender: TObject)
private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);const MAXVALUE = 99999;var Index, iRow: Integer
iTmp,I: Integer
procedure DoAdd
begin Listbox2.Items.Add(inttostr(Index))
end;begin iRow := 1
Index := 1
Listbox2.Items.BeginUpdate
try while (Index <= MAXVALUE) and (iRow <= Listbox1.Count) do begin iTmp := StrToInt(Listbox1.Items[iRow - 1])
if iTmp <> Index then begin DoAdd()
end else Inc(iRow)
Inc(Index)
end
while (Index <= MAXVALUE) do begin DoAdd
Inc(Index)
end
finally Listbox2.Items.EndUpdate
end;end;procedure TForm1.Button3Click(Sender: TObject);var I: Integer;begin Listbox1.Clear
Listbox2.Clear
Listbox1.Items.BeginUpdate
for I := 1 to 40000 do Listbox1.Items.Add(inttostr(i * 2))
Listbox1.Items.EndUpdate;end;end.---------------------dfm:-------------object Form1: TForm1 Left = 268 Top = 273 Width = 675 Height = 392 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 40 Top = 24 Width = 129 Height = 177 ItemHeight = 13 Items.Strings = ( '00003' '00005' '00006') TabOrder = 0 end object ListBox2: TListBox Left = 320 Top = 8 Width = 129 Height = 153 ItemHeight = 13 TabOrder = 1 end object Button2: TButton Left = 216 Top = 40 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 2 OnClick = Button2Click end object Button3: TButton Left = 216 Top = 152 Width = 75 Height = 25 Caption = 'Button3' TabOrder = 3 OnClick = Button3Click endend
 
或者你把 Index := 1
更改为 Index := 0;刚才试了一下,如果ListBox中是从0开始的,就会出现你说的问题。
 
znxia,good好用
 
如何限定死它必须只取5位数呢,因为我这个listbox1里肯定只有5位数字
 
既然已经肯定listbox1里肯定只有5位数字 ,还要限定死它必须只取5位数?没完全理解,呵呵
 
你的这两种写法都很不错,高手啊。我更喜欢后面那种,如果能加上限定死5位数字就好了Listbox1.Items.Add(inttostr(i * 2))
这句能不能说明一下
 
哦,那是生成测试数据用的。可以更改为:procedure TForm1.Button3Click(Sender: TObject);var I: Integer
S:String;begin Listbox1.Clear
Listbox2.Clear
Listbox1.Items.BeginUpdate
for I := 1 to 40000 do begin S:='000000'+InttoStr(I*2)
S:= Copy (S,length(S)-4, 5)
Listbox1.Items.Add( S )
end
Listbox1.Items.EndUpdate;end;
 
感谢znxia,不错
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
950
SUNSTONE的Delphi笔记
S
S
回复
0
查看
774
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部