一维数组中有两个数相同,怎么用最短的时间找出来啊!
别让我一个一个比较啊!
别让我一个一个比较啊!
解决方案 »
- 管道模式的多个处理过程,能否自动优化为各个过程处理每一部分就交给下一个过程开始处理?
- 制作ActiveX控件
- 怎么使自己的程序可以像WINAMP那样支持插件方式的扩展,使用什么技术/
- 哪里有关于网上任意两台计算机点对点文件互传的资料或控件?
- 天杀的QuickRep,高分救急,问题2。
- 用过fastreport的朋友请进。。。
- 谁会写生成收藏夹菜单的代码?
- 我再贴:寻人,请问的mail是什么啊?我有问题请教
- 高分求解:Ado使用过程中导致内存泄漏问题。
- 有谁用DELPHI+ADO(或者BDE)编过MTS三层结构的程序,能否共享一下
- 三层的小问题,请进~ 一定要进,帮帮忙啊
- 找不到工作,第一次散分,外+一个问题!
begin
..............//Initialize your aa and ba for i:=1 to 100 do
for n:=1 to 100 do
if aa[i]=ba[n] then showmessage(format('Found in aa[%d] and ba[%d]',[i,n]));
end;
除了这样挨个比较就没有好办法了吗?
var
aa: array [1..100] of integer;
i, j: integer;
begin
// init array aa
for i := 1 to 100 do
for j := 100 downto i + 1 do
if aa[i] = aa[j] then ShowMessage(format('Found in aa[%d] and ba[%d]',[i,n]))
end;
aa:array[1..100] of integer;ba:array[1..100] of integer;i,n:integer;
bFound: Boolean;
begin
..............//Initialize your aa and ba bFound := False;
for I := Low(aa) to High(aa) do
for J := Low(aa) to High(aa) do
begin
if I = J then continue;
if aa[I] = aa[J] then
begin
bFound := True;
Break;
end;
end;
if bFound then Break;
end;
if bFound then ShowMessage(format('Found in aa[%d] and ba[%d]',[i,n]))
else ShowMessage('....');
end;
IntegerArray : Integer[1..100] of Integer;
i : Integer;
IntegerSet : Set of 0..255;begin
i:=1;
repeat
IntegerSet := Integer + [IntegerArray[i]];
inc(i);
until (IntegerArray[i] in IntegerSet) or (i > High(IntegerArray));
if i < high(IntegerArray) then
writeln(IntegerArray[i])
else
writeln("All Elements in Array are unique!")
end;
{
if a[i]==a[j] then break;
}
i:=0;
j:=元素个数;
while i<j do
begin
if a[i]=a[j] then break;
inc(i);
dec(j);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a:array[0..15]of integer;
i,j:integer;
begin
for i:=0 to 7 do
a[i]:=i;
for i:=15 downto 7 do
a[i]:=i-7;
for i:=0 to 15 do
ListBox2.Items.Append(inttostr(a[i]));
for i:=0 to high(a) do
for j:=high(a) downto 0 do
begin
if i=j then continue;
if a[i]=a[j] then ListBox1.Items.Append(inttostr(a[i]));
end;
end;
SourceArray: array[1..100] of Integer;
IntArray : array of Integer;
max : Integer;
i : Integer;begin
max := 0;
for i:=1 to high(SourceArray) do
begin
if max < SourceArray[i] then max := SourceArray[i];
inc(i);
end;
SetLength(IntArray, max+1);
for i:=1 to high(SourceArray) do
begin
inc(IntArray[SourceArray[i]]);
end;
i:= 0;
while i<= high(IntArray) do
begin
if IntArray[i] >= 2 then writeln(""+ IntArray[i]);
inc(i);
end;
end;这个算法要作三次循环。第一次,找出数组中最大的数。然后建立一个新的,容量与这个最大的数相同的数组。第二次遍历源数组,每次找到一个数字就让新建的那个数组中的,下标为这个数字的元素加一。第三次遍历新建数组,找出新建数组中所有大于等于2的元素,然后可以说这些数字重复了超过两遍。
SourceArray: array[1..100] of Integer;
IntArray : array of Integer;
max : Integer;
i : Integer;begin
max := 0;
for i:=1 to high(SourceArray) do
if max < SourceArray[i] then max := SourceArray[i];
SetLength(IntArray, max+1);
for i:=1 to high(SourceArray) do inc(IntArray[SourceArray[i]]);
i:= 0;
for i:= 0 to high(IntArray) do
if IntArray[i] >= 2 then writeln(""+ IntArray[i]);
end;
var
s : string;
i : integer;
begin
s := '';
for i := low(aarray) to high(aarray) do begin
if Pos(InttoStr(aarray[i],s) > 0 then begin
ShowMessage('Found!');
Break;
end else
s := s + InttoStr(aarray[i])+' '
end;
PItem = ^Item;
TItem = record
Index: Integer;
Next: PItem;
end;var
arrMap: array [0..255] of PItem;procedure InitialMap;
begin
FillChar(arrMap, SizeOf(arrMap), 0);
end;procedure FreeMap;
begin
// ??? ... ...
end;function Add(AIndex, AValue: Integer; AList: array of Integer): Integer;
var
intItem: Integer;
pCurrent: PItem;
begin
intItem := AValue mod 256;
pCurrent := arrMap[intItem];
result := -1;
while pCurrent <> nil do
begin
if AList[pCurrent^.Index] = AValue then
begin
result := pCurrent^.Index;
Break;
end;
pCurrent := pCurrent^.Next;
end; // No Exist, Add to Map
if result = -1 then
begin
New(pCurrent);
pCurrent^.Index := AIndex;
pCurrent^.Next := arrMap[intItem];
arrMap[intItem] := pCurrent;
end;
end;function FindEqualIndex(AList: array of Integer; var A, B: Integer): Boolean;
var
intNo: Integer;
begin
A := -1;
B := -1;
InitialMap;
result := False;
try
for intNo := 0 to High(AList) do
begin
A := Add(intNo, AList[intNo], AList);
if A >= 0 then
begin
B := intNo;
result := True;
Break;
end;
end;
finally
FreeMap;
end;
end;
pos是内部函数,用汇编写的,所以速度是最快的,肯定比你用高级语言写的任何同功能的函数速度要快.
内存也可以预先根据项数 GetMem,自己管理相对地址即可,这样效率会更高。
lea esi, Source // 假设 Source 数据尺寸为 $7FFFFFFF
lea edi, Dest
mov ecx, $7FFFFFFF
rep movsb以上虽然指令简单,但是所需时间比mov al, Source[0]
mov Dest[0], al
...
mov al, Source[999]
mov Dest[999], al要大得多,我只想说明 Pos 函数虽然很快,但其时间复杂度是存在的,不可逃避的现实。
尽量不要使用borland给咱们的函数--那不如考虑并行处理,这样可以成倍提高算法效率.
如 a[0] := 123456; a[1] := 3456;
用你的算法回认为两者相同