Home > Back-end >  Help to take a look at this algorithm
Help to take a look at this algorithm

Time:10-03

The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

Type
TForm1=class (TForm)
For: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
A: an array of Integer;
Implementation

{$R *. DFM}
{with backtracking method is divided into two groups, a group of several in each a set of several similar to half of the group number of total}
Procedure TForm1. Button1Click (Sender: TObject);
Var
I, j, n, sum: Integer;
Target: Double;
Array1, array2: an array of Integer;
TempTarget: Integer;
CloseValue tempSub: Double;
TempValue: Integer;
Replaced: Boolean;
S, sp: string;
The begin
N:=0;
S:=Trim (Edit7. Text);
//Form1. Caption:=IntToStr (Length (s));
//the number of figures
For I:=0 to Length (s) - 1 do
The begin
If s=[I] ', 'then
N:=n + 1;
end;
SetLength (a, n + 1);
I:=0;
N:=1;
J:=0;
Sp:=' ';
While i<=Length (s) do
The begin
{to ', 'for the node number assigned to the array}
If s=[I] ', 'then
The begin
A: [j]=StrToInt (Copy (s, n, I - n));
J:=j + 1;
N:=I + 1;//a ', 'the number of + 1
end;
I:=I + 1;
end;
Sum:=0;
For I:=Low to High (a) (a) do
The begin
Sum:=sum + a [I];//calculate the Numbers, and
//Form1. Caption:=IntToStr (High) (a) + '+' + IntToStr (Low (a));
end;
Target:=sum/2;//the group number and half of the
Div 2 n=Length (a);
SetLength (array1, n);
SetLength (array2, n);
Do the for I:=0 to n - 1
The begin
Array1 [I] :=a, [I].//is divided into two groups, the former half assignment
Array2: [I]=[I + n]//after a half assignment, so add n
end;
while True do
The begin
TempTarget:=0;
//for first half and
Do the for I:=0 to n - 1
The begin
TempTarget: tempTarget + array1=[I];
end;
CloseValue:=target - tempTarget;//the target (the sum of the number of half) minus the first half and
Replaced:=False;
Do the for I:=0 to n - 1
The begin
Do the for j:=0 to n - 1
The begin
TempSub: array2=[j] - array1 [I];
If (tempSub & lt; 0) and (CloseValue & lt; 0) then
The begin
TempSub:=- tempSub;
CloseValue:=- CloseValue;
end;
If (Abs (CloseValue - tempSub) & lt; CloseValue) and (tempSub & gt;=0) then
The begin
TempValue: array1=[I];
Array1 [I] : array2=[j];
[j] : array2=tempValue;
Replaced:=True;
Break;
end;
end;
If Replaced then Break;
end;
If not Replaced then Break;

end;
Edit3. Text:=' ';
Edit4. Text:=' ';
TempTarget:=0;
Do the for I:=0 to n - 1
The begin
Edit3. Text:=Edit3. Text + IntToStr (array1 [I]) + '+';
Edit4. Text:=Edit4) Text + IntToStr (array2 [I]) + '+';
TempTarget: tempTarget + array1=[I];
end;
Edit5. Text:=IntToStr (tempTarget);
Edit6. Text:=IntToStr (sum - tempTarget);
Edit1. Text:=IntToStr (sum);
Edit2. Text:=FloatToStr (target);
end;

End.

CodePudding user response:

Don't understand the red part of the algorithm, especially two running the if condition
  • Related