• 全部
• 语言基础/算法/系统设计
• 数据库相关
• 图形处理/多媒体
• 网络通信/分布式开发
• VCL组件开发及应用
• Windows SDK/API
• 问答

求教一个算法，急等帮助

sharklee 2001-11-22 11:44:44

...全文
63 点赞 收藏 8

8 条回复

//用组合就可以了
//借用一下二进制

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

uses
Math;

const
cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string;
var
I, J: Integer;
begin
Result := '';
I := mNumber;
while (I >= mScale) and (mScale > 1) do begin
J := I mod mScale;
I := I div mScale;
Result := cScaleChar[J + 1] + Result;
end;
Result := cScaleChar[I + 1] + Result;
if mLength > 0 then
for I := 1 to mLength - Length(Result) do
Result := '0' + Result;
end; { IntToDigit }

function DigitToInt(mDigit: string; mScale: Byte): Integer;
var
I: Byte;
L: Integer;
begin
Result := 0;
L := Length(mDigit);
for I := 1 to L do
Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *
Trunc(IntPower(mScale, I - 1));
end; { DigitToInt }

function f(mPrices: array of Real; mMoney: Real): string;
var
I, J: Integer;
L: Integer;
S, C: string;
T: Real;
begin
L := Length(mPrices);
with TStringList.Create do try
for I := 0 to Trunc(IntPower(2, L)) - 1 do begin
S := IntToDigit(I, 2, L);
T := 0;
C := '';
for J := 1 to L do if S[J] = '1' then begin
T := T + mPrices[J - 1];
C := C + ',' + FloatToStr(mPrices[J - 1]);
end;
System.Delete(C, 1, 1);
if T = mMoney then
end;
Result := Text;
finally
Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := f([1, 2, 3, 4, 5, 6, 7], 20);
end;

end.

superatom 2001-11-22
1、首先将大于S的书排除出去；
2、用循环把书价进行组合，找出符合条件的组合

sharklee 2001-11-22

scripting 2001-11-22

。。。。。。。

byrybye 2001-11-22

sharklee 2001-11-22

Delphi

4875

Delphi 开发及应用

2001-11-22 11:44