View Full Version : Help Creating Compex Function;
Bad Boy JH
09-04-2010, 10:12 AM
Difficult Problem...
I have 3 variables
ThreeVotes
FiveVotes
TenVotes
Worth 3, 5, and 10 votes each, respectivly. What I need is a function that gets a combination of those votes, that adds up to 55, using as many 3s, and a few 10s as possible, If they canot add to exactly 55, it should be slightly over...
Needs to return the amounts of each vote used...
EvilChicken!
09-04-2010, 11:22 AM
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.
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.
:)
Bad Boy JH
09-04-2010, 11:44 AM
Sort of, But I needs to prioritise the 55 votes thing...
var
Vote10Used, Vote10UnUsed, Vote5Used, Vote5UnUsed, Vote3Used, Vote3UnUsed: Integer
begin
Vote10UnUseed := TenVote;
Vote5UnUsed := FiveVote - 2;
Vote3UnUsed := ThreeVote - 15;
Vote3Used := 15;
Vote5Used := 2;
While Vote3Unused < 0 do
begin
DecEx(Vote3Used, 5);
IncEx(Vote3UnUsed, 5);
DecEx(Vote5UnUsed, 3);
IncEx(Vote5Used, 3);
end;
While Vote5UnUsed < 0 do
begin
if Vote10UnUsed > 0 then
begin
Inc(Vote10Used);
Dec(Vote10UnUsed);
IncEx(Vote5UnUsed, 2);
DecEx(Vote5Used, 2);
end else begin
DecEx(Vote3Used, Vote5UnUsed * 2);
IncEx(Vote3UnUses, Vote5UnUsed * 2);
DecEx(Vote5UnUsed, Vote5UnUsed);
IncEx(Vote5Used, Vote5UnUsed);
end;
end;
end;
What about that, Anyone see a problem with that?
EvilChicken!
09-04-2010, 11:51 AM
I needs to prioritise the 55 votes thing...
>_<
You should have mentioned that getting it to become as close to 55 votes as possible was top priority.
nielsie95
09-04-2010, 12:02 PM
Here's for exactly 55:
program New;
const
Votes3 = 40;
Votes5 = 0;
Votes10 = 2;
var
i, m: Integer;
v: array[0..2] of Integer;
begin
for i := 3 downto 0 do
begin
if (Floor(Votes3 / 5.0) < i) then
Continue;
v[0] := i * 5;
m := Floor((55 - (i * 15)) / 5.0);
if (Votes5 < m) then
begin
if ((m mod 2) = 1) then
begin
if (Votes5 < 1) then
Continue;
v[1] := Votes5 - 1;
m := Floor((m - (Votes5 - 1)) / 2.0)
end
else
begin
v[1] := Votes5;
m := Floor((m - Votes5) / 2.0);
end;
if (Votes10 < m) then
begin
WriteLn('Impossible');
Exit;
end;
v[2] := m;
Break;
end
else
begin
v[1] := m;
Break;
end;
end;
if (v[0] * 3 + v[1] * 5 + v[2] * 10 <> 55) then
begin
WriteLn('Impossible');
Exit;
end;
WriteLn(Format('Votes3: %d -- Votes5: %d -- Votes10: %d', [v[0], v[1], v[2]]));
end.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.