unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Private
{ Private declarations }
Public
{ Public declarations }
end;
TarrInt = array of Integer;
TArrArrInt = array of TarrInt;
var
Form1: TForm1;
inA: Tarrint;
function GetBest(aai: Tarrint): TArrarrint;
function GetNum(aai: TArrint): Integer;
implementation
{$R *.DFM}
function GetNum(aai: TArrint): Integer;
var
tmp: Tarrarrint;
i, j, h, rlt: Integer;
b: Boolean;
begin
rlt := 0;
while Length(aai) > 0do
begin
tmp := GetBest(aai);
i := 0;
while i <= High(aai)do
begin
b := False;
for h := 0 to High(tmp[0])do
if aai[i] = tmp[0, h] then
b := True;
if b then
begin
for j := i + 1 to High(aai)do
aai[j - 1] := aai[j];
setlength(aai, Length(aai) - 1);
Dec(i);
end;
inc(i);
end;
inc(rlt);
end;
Result := rlt;
end;
function GetBest(aai: Tarrint): TArrarrint;
var
i, j,h, maxa,ll: Integer;
aaa: array of Tarrint;
rlt: Tarrarrint;
begin
//389 207 155 300 299 170 158 65
ll := Length(aai);
setlength(aaa, ll*ll);
for i := 0 to High(aai)do
for h := 1 to High(aai)-ido
begin
setlength(aaa[i*Ll+h], 1);
aaa[i*ll+h, 0] := aai[i];
for j := i + h to High(aai)do
if aai[j] <= aaa[i*ll+h,High(aaa[i*ll+h])] then
begin
setlength(aaa[i*ll+h], Length(aaa[i*ll+h]) + 1);
aaa[i*ll+h, High(aaa[i*ll+h])] := aai[j];
end;
end;
setlength(rlt, 0);
maxa := 0;
for i := 0 to High(aaa)do
if maxa < Length(aaa[i]) then
maxa := Length(aaa[i]);
for i := 0 to high(aaa)do
if Length(aaa[i]) = maxa then
begin
setlength(rlt, Length(rlt) + 1);
rlt[High(rlt)] := aaa[i];
end;
Result := rlt;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ss: tarrarrint;
iii : Integer;
begin
setLength(ina, 8);
ina[0] := 389;
ina[1] := 207;
ina[2] := 155;
ina[3] := 300;
ina[4] := 299;
ina[5] := 170;
ina[6] := 158;
ina[7] := 65;
ss := GetBest(ina);
iii := GetNum(ina);
ShowMessage('有' + IntToStr(Length(ss)) + '种组合,最多可打下' + IntToStr(Length(ss[0])) + '导弹'#10#13'最少需要系统数为'+inttostr(iii));
end;
end.