33,008
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
lblX: TLabel;
Label1: TLabel;
edtX: TEdit;
edtY: TEdit;
lblCnt: TLabel;
chkDetail: TCheckBox;
btnCalcu: TButton;
mmo1: TMemo;
procedure btnCalcuClick(Sender: TObject);
private
procedure CalcuIDX( tyerID : Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
K1 : array[1..7] of Integer = (1,1,2,0,3,2,0); //式1系数
K2 : array[1..7] of Integer = (0,1,0,2,0,1,3); //式2系数
MaxV : array[1..7] of Integer; // 结果 最大取值
Value : array[1..7] of Integer; // 结果(a,b,c,d,e,f,g)
xV : Integer;
yV : Integer;
RsltCnt : Integer; //解 组数
implementation
{$R *.dfm}
function GetDivValue (sum , Cnt : Integer) : Integer;
begin
if Cnt = 0 then
Cnt := 1;
Result := sum div Cnt;
end;
procedure TForm1.CalcuIDX( tyerID : Integer);
var
idx : Integer;
tmpSum1 ,tmpSum2 : Integer;
sRslt : string;
begin
Application.ProcessMessages();
tmpSum1 := 0;
tmpSum2 := 0;
for idx := 1 to tyerID - 1 do
begin
tmpSum1 := tmpSum1 + K1[idx] * Value[idx];
tmpSum2 := tmpSum2 + K2[idx] * Value[idx];
end;
if (tmpSum1 > xV) or (tmpSum2 > yV) then
Exit;
if (tyerID = 7 ) then
begin
tmpSum1 := tmpSum1 + K1[7] * Value[7];
tmpSum2 := tmpSum2 + K2[7] * Value[7];
if (tmpSum1 = xV) and (tmpSum2 = yV) then
begin
Inc( RsltCnt);
if not chkDetail.Checked then
lblCnt.Caption := IntToStr(RsltCnt)
else
begin
sRslt := IntToStr(RsltCnt) + '、';
for idx := 1 to 7 do
sRslt := sRslt + IntToStr(Value[idx]) + ',';
mmo1.Lines.Add(sRslt);
end;
end;
end
else
for idx := 0 to MaxV[tyerID] do
begin
Value[tyerID] := idx;
CalcuIDX(tyerID+1);
end;
end;
procedure TForm1.btnCalcuClick(Sender: TObject);
var
idx : Integer;
tmpV1 : Integer;
tmpV2 : Integer;
begin
xV := StrToInt(edtX.Text);
yV := StrToInt(edtY.Text);
for idx := 1 to 7 do
begin
tmpV1 := GetDivValue(xV,K1[idx]) ;
tmpV2 := GetDivValue(yV,K2[idx]) ;
//从两者中取一个最小的值作为结果上限
if (tmpV1 > tmpV2) then
MaxV[idx] := tmpV2
else
MaxV[idx] := tmpV1;
end;
mmo1.Lines.Clear;
lblCnt.Caption := '0';
mmo1.Lines.Add('开始……');
RsltCnt := 0;
CalcuIDX(1);
mmo1.Lines.Add('完成!');
end;
end.