Results 1 to 5 of 5

Thread: Help Creating Compex Function;

  1. #1
    Join Date
    Dec 2009
    Location
    Newcastle, Australia
    Posts
    888
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Help Creating Compex Function;

    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...

    Current Script Project
    Pot of flour gatherer - 95% done

    Can't get Simba to work? Click here for a tutorial

  2. #2
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,938
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    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.


  3. #3
    Join Date
    Dec 2009
    Location
    Newcastle, Australia
    Posts
    888
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Sort of, But I needs to prioritise the 55 votes thing...
    SCAR Code:
    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?

    Current Script Project
    Pot of flour gatherer - 95% done

    Can't get Simba to work? Click here for a tutorial

  4. #4
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,938
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Bad Boy JH View Post
    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.

  5. #5
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Here's for exactly 55:

    Simba Code:
    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.
    Hup Holland Hup!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •