to hamzsy:
10 -> 10
20 -> 20
30 -> 10 + 20
40 -> 20 + 20
50 -> 50
60 -> 50 + 10
70 -> 50 + 20
80 -> 50 + 20 + 10
90 -> 50 + 20 + 20
100 -> 50 + 50
100.9 -> 50 + 50 + 0.50 + 0.20 + 0.20
你说的是这样?
10 -> 10
20 -> 20
30 -> 10 + 20
40 -> 20 + 20
50 -> 50
60 -> 50 + 10
70 -> 50 + 20
80 -> 50 + 20 + 10
90 -> 50 + 20 + 20
100 -> 50 + 50
100.9 -> 50 + 50 + 0.50 + 0.20 + 0.20
你说的是这样?
100 -> 100
100.9 -> 100 + 0.50 + 0.20 + 0.20
比如有¥0.05、¥0.1、¥0.5、¥1、¥2、¥5、¥10、¥20、¥50、¥100
要找给客户150元,应该是1*¥100+1*50。但有可能¥50的没了,就应该是1*100+2*¥20+1*¥10,再有就是连20的只剩下1张了,就应该是1*¥100+1*¥20+3*10
零钱列举
100.00、50.00、20.00、10.00、5.00、2.00、1.00、0.50、0.20、0.10、0.05、0.02、0.01
从最大的开始分解,这样的数目就是最小的
//*)function Calc(mMoney: Real): string;
const
cMoneyList: array[0..12] of Real = (
100.00,50.00,20.00,10.00,5.00,2.00,1.00,0.50,0.20,0.10,0.05,0.02,0.01);
var
I: Integer;
T: Integer;
vMoney: Real;
begin
vMoney := mMoney;
Result := '';
for I := Low(cMoneyList) to High(cMoneyList) do begin
if vMoney < 0.01 then Break;
T := Trunc(vMoney / cMoneyList);
if T > 0 then begin
Result := Format('%s+%d*%.2f', [Result, T, cMoneyList[I]]);
vMoney := vMoney - T * cMoneyList[I];
end;
end;
Delete(Result, 1, 1);
end;
好像钱的张数是有限的----》
“但有可能¥50的没了,就应该是1*100+2*¥20+1*¥10”
const
cMoneyList: array[0..12] of Real = (
100.00,50.00,20.00,10.00,5.00,2.00,1.00,0.50,0.20,0.10,0.05,0.02,0.01);
cCountList: array[0..12] of Real = (
1,2,3,4,5,6,7,8,9,10,11,12,13);
var
I: Integer;
T: Integer;
vMoney: Real;
begin
vMoney := mMoney;
Result := '';
for I := Low(cMoneyList) to High(cMoneyList) do begin
if vMoney < 0.01 then Break;
T := Max(Trunc(vMoney / cMoneyList), cCountList[I]); //Changed
if T > 0 then begin
Result := Format('%s+%d*%.2f', [Result, T, cMoneyList[I]]);
vMoney := vMoney - T * cMoneyList[I];
end;
end;
Delete(Result, 1, 1);
end;
钱面值特殊一点;我看不能這樣理解,我認為應該钱面值是定下來的,不是隨便修改的
TChangesRec = record
A50,
A20,
A10,
A5,
A2,
A1,
A50Cent,
A20Cent,
A10Cent,
A5Cent,
A2Cent,
A1Cent
: integer;
end;
end;function CashToChanges(Cash: word; ChangesRec: PChangesRec): word;
begin
Result := Cash; // Assign value to Result
{ Calculate the values for quarters, dimes, nickels, pennies }
with ChangesRec^ do
begin
A50 := Cash div 50;
Cash := Cash - A50 * 25;
A20 := Cash div 20;
Cash := Cash - A20 * 20;
A10 := Cash div 10;
Cash := Cash - A10 * 5;
...
A1Cent := Cash div 0.01;
end;
end;
正常情况下CashToChanges返回0,否则还有1分以下的
实际零钱数目为A50+A20+...+A1Cent.
uses Math;function Calc(mMoney: Real): string;
const
cMoneyList: array[0..12] of Real = (
100.00,50.00,20.00,10.00,5.00,2.00,1.00,0.50,0.20,0.10,0.05,0.02,0.01);
cCountList: array[0..12] of Real = (
1,2,3,4,5,6,7,8,9,10,11,12,13); //对应零钱的张数
var
I: Integer;
T: Integer;
vMoney: Real;
begin
vMoney := mMoney;
Result := '';
for I := Low(cMoneyList) to High(cMoneyList) do begin
if vMoney < 0.01 then Break;
T := Min(Trunc(vMoney / cMoneyList), cCountList[I]); //Max -> Min
if T > 0 then begin
Result := Format('%s+%d*%.2f', [Result, T, cMoneyList[I]]);
vMoney := vMoney - T * cMoneyList[I];
end;
end;
Delete(Result, 1, 1);
end;
if vMoney >= 0.01 then Result := Result + '+?';
Delete(Result, 1, 1);
end;//...
只是这段代码没有经过调试
还有其他情况也要考虑
换多次的情况,这样零钱的张数还要处理
不过思路有了,要实现也很简单
例:
x:=150 div 100;
y:=150 mod 100;
if y=0 then 钱的张数为x
else
x1:=y div 50;
y1:=y mod 50;
...............
uses Math;const
cMoneyList: array[0..12] of Real = (
100.00, 050.00, 020.00, 010.00, 005.00,
002.00, 001.00, 000.50, 000.20, 000.10,
000.05, 000.02, 000.01);type
TCountList = array[0..12] of Integer; //零钱的张数类型var
vCountList: TCountList;function Calc(mMoney: Real; var nCountList: TCountList): string;
var
I, T: Integer;
vMoney: Real; //余钱
begin
Result := '';
vMoney := mMoney;
for I := Low(cMoneyList) to High(cMoneyList) do begin
if vMoney < cMoneyList[High(cMoneyList)] then Break;
T := Min(Trunc((vMoney / cMoneyList[I]) + 1E-5), nCountList[I]); if T > 0 then begin
Result := Format('%s + %d * ¥%6.2f', [Result, T, cMoneyList[I]]);
nCountList[I] := nCountList[I] - T; //减去张数
vMoney := vMoney - T * cMoneyList[I];
end;
end;
if vMoney >= cMoneyList[High(cMoneyList)] then
Result := Result + ' + ????';
Delete(Result, 1, 3);
end;function StrToFloatDef(mStr: string; mDefault: Real): Real;
var
E: Integer;
begin
Val(mStr, Result, E);
if E <> 0 then Result := mDefault;
end;procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
T, L: Integer;
begin
for I := Low(cMoneyList) to High(cMoneyList) do begin
with TSpinEdit.Create(Self) do begin
Parent := Self;
T := I * (Height + 2);
L := Left + Width;
Top := T;
Value := 100;
Name := Format('SpinEdit%d', [I]);
end;
with TLabel.Create(Self) do begin
Parent := Self;
Top := T + 2;
Caption := FormatFloat('¥000.00', cMoneyList[I]);
Left := L + 2;
Name := Format('Label%d', [I]);
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
///////Begin 获得零钱张数
for I := Low(vCountList) to High(vCountList) do
if FindComponent(Format('SpinEdit%d', [I])) is TSpinEdit then
vCountList[I] :=
TSpinEdit(FindComponent(Format('SpinEdit%d', [I]))).Value;
///////End 获得零钱张数
Memo2.Clear;
for I := 0 to Memo1.Lines.Count - 1 do
Memo2.Lines.Add(Format('%s = %s', [Memo1.Lines[I],
Calc(StrToFloatDef(Memo1.Lines[0], 0), vCountList)]));
end;
del_c_sharp(头大中......)的建议很好
感谢各位了
结帖了