📄 unit1.~pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Button1: TButton;
Panel3: TPanel;
Label4: TLabel;
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Panel4: TPanel;
StringGrid2: TStringGrid;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type array2r=array of array of real;
type array2i=array of array of integer;
var
Form1: TForm1;
n:integer;
a:real;
tt:integer;
i1,ii:array2r;
o1,oo:array2r;
c1,cc:array2r;
g:array2r;
x:array2i;
temp1:integer;
temp2:integer;
temp3:integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j:integer;
begin
n:=strtoint(edit1.Text);
a:=strtofloat(edit2.Text);
tt:=strtoint(edit3.text);
temp3:=round(n*(n+1)/2);
stringgrid1.RowCount:=4;
stringgrid1.ColCount:=temp3+n+1;
stringgrid2.RowCount:=3;
stringgrid2.ColCount:=n+1;
SetLength(i1,n+1,n+1);
SetLength(o1,n+1,n+1);
SetLength(c1,n+1,n+1);
SetLength(ii,n+1,n+1);
SetLength(oo,n+1,n+1);
SetLength(cc,n+1,n+1);
SetLength(g,n+2,n+2);
SetLength(x,n+1,n+1);
temp1:=0;
temp2:=0;
for i:=1 to n do
begin
temp1:=temp2+1;
temp2:=temp2+n+1-i;
for j:=temp1 to temp2 do
stringgrid1.Cells[j,0]:=inttostr(j-temp1);
end;
for j:=temp3+1 to temp3+n do
stringgrid1.Cells[j,0]:=inttostr(j-temp3);
stringgrid1.Cells[0,1]:='收入';
stringgrid1.Cells[0,2]:='运行费用';
stringgrid1.Cells[0,3]:='更新费用';
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i,j:integer;
begin
temp1:=0;
temp2:=0;
for i:=1 to n do
begin
temp1:=temp2+1;
temp2:=temp2+n+1-i;
for j:=temp1 to temp2 do
begin
i1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,1]);
o1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,2]);
c1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,3]);
end;
end;
for j:=temp3+1 to temp3+n do
begin
i1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,1]);
o1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,2]);
c1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,3]);
end;
for i:=1 to n do
for j:=0 to i do
begin
ii[i,j]:=i1[i-j,j];
oo[i,j]:=o1[i-j,j];
cc[i,j]:=c1[i-j,j];
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
j:integer;
t:integer;
begin
for t:=1 to n+1 do
g[n+1,t]:=0;
for j:=n downto 1 do
for t:=1 to j+tt-1 do
begin
g[j,t]:=ii[j,0]-oo[j,0]-cc[j,t]+a*g[j+1,1];
x[j,t]:=-1;
if ii[j,t]-oo[j,t]+a*g[j+1,t+1]>g[j,t] then
begin
g[j,t]:=ii[j,t]-oo[j,t]+a*g[j+1,t+1];
x[j,t]:=1;
end;
end;
stringgrid2.Cells[0,0]:='年';
stringgrid2.Cells[0,1]:='机龄';
stringgrid2.Cells[0,2]:='最佳策略';
for j:=1 to n do
stringgrid2.Cells[j,0]:=inttostr(j);
stringgrid2.Cells[1,1]:=inttostr(1);
if x[1,1]=1 then
stringgrid2.Cells[1,2]:='保持'
else stringgrid2.Cells[1,2]:='更新';
t:=1;
for j:=1 to n-1 do
begin
if x[j,t]=1 then
begin
t:=t+1;
stringgrid2.cells[j+1,1]:=inttostr(t);
if x[j+1,t]=1 then stringgrid2.Cells[j+1,2]:='保持'
else stringgrid2.Cells[j+1,2]:='更新';
end
else
begin
t:=1;
stringgrid2.cells[j+1,1]:=inttostr(t);
if x[j+1,t]=1 then stringgrid2.Cells[j+1,2]:='保持'
else stringgrid2.Cells[j+1,2]:='更新';
end;
end;
label6.Caption:='相应的最佳收益为:'+floattostr(g[1,1])+#13;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -