Simba Code:
const
DESIRED_TOTAL = 55;
function CountVotes(var ThreeVotes, FiveVotes, TenVotes: Integer): Integer;
var
Total, I, _3, _5, _10: Integer;
begin
try
_3 := ThreeVotes;
for I := 1 to _3 do
begin
IncEx(Total, 3);
ThreeVotes := I;
if (Total >= DESIRED_TOTAL) then
begin
FiveVotes := 0;
TenVotes := 0;
Exit;
end;
end;
_5 := FiveVotes;
for I := 0 to _5 do
begin
IncEx(Total, 5);
FiveVotes := I;
if (Total >= DESIRED_TOTAL) then
begin
TenVotes := 0;
Exit;
end;
end;
_10 := TenVotes;
for I := 0 to _10 do
begin
IncEx(Total, 10);
TenVotes := I;
if (Total >= DESIRED_TOTAL) then
Exit;
end;
finally
Result := Total;
end;
end;
var
Three, Five, Ten, Res: Integer;
begin
Three := 2;
Five := 5;
Ten := 100;
Res := CountVotes(Three, Five, Ten);
WriteLn('Reached a total of ' + ToStr(Res) + ' using #' +
ToStr(Three) + ' votes worth 3, #' + ToStr(Five) +
' votes worth 5 and #' + ToStr(Ten) + ' votes worth 10.');
end.
Progress Report:
Reached a total of 56 using #2 votes worth 3, #5 votes worth 5 and #1 votes worth 10.
This is my interpretation of your request.. Is it anything like you wanted?
I didn't try to shorten it so you could understand the code more easily -- although shortening would be easily accomplished by combining all the elements in one array and using a for-to-loop.