Here is my latest attempt.  I would appreciate any comments, particularly about 
how to better take advantage of Unicon's strengths.


Thanks, Steve

===

global coins, numLevels, targetSum, totalCoins, tries

procedure main()
   local cumulativeSum, level, numCoinsLeft
   init()
   level         :=  1
   numCoinsLeft  := totalCoins
   cumulativeSum :=  0
   solveProblem(level,numCoinsLeft,cumulativeSum)
   write("\n# Tries = ",tries)
end
   
procedure init()
   coins      := [
                  ["Dollar",100,0,"Dollars"],
                  ["Half Dollar",50,0,"Half Dollars"],
                  ["Quarter",25,0,"Quarters"],
                  ["Dime",10,0,"Dimes"],
                  ["Nickel",5,0,"Nickels"],
                  ["Penny",1,0,"Pennies"]
                 ]
   numLevels  := *coins
   targetSum  := 110
   totalCoins := 13
   tries      := 0
end

procedure solveProblem(level,numCoinsLeft,cumulativeSum)
   local aList, i, newSum
   tries    +:= 1
   every i   := numCoinsLeft to 0 by -1 do {
      newSum := cumulativeSum + (i * coins[level][2])
      if newSum = targetSum then {
         if numCoinsLeft = i then {
            aList        := coins[level]
              aList[3]     := i
              coins[level] := aList
              printSolution(level)
              aList[3]     := 0
              coins[level] := aList
              }
           }
      else {
         if newSum < targetSum then {
            if (level < numLevels) & ((numCoinsLeft - i) > 0) then {
                 aList        := coins[level]
                 aList[3]     := i
                 coins[level] := aList
                 solveProblem(level + 1, numCoinsLeft - i, newSum)
                 aList[3]     := 0
                 coins[level] := aList
                 }
            }
         }
      }
end

procedure printSolution(level)
   local aList, i
   write("")
   every i  := 1 to level do {
      aList := coins[i]
      if aList[3] > 0 then {
         writes(right(aList[3],2), " ")
         writes(left((aList[3] = 1 & aList[1]) | aList[4],13))
           }
      }
   write("")
end

===

>>> Steve Wampler <[EMAIL PROTECTED]> 10/31/05 12:20 PM >>>
There was a lot of unneeded code in that last solution (left over from
the previous one), so here's a cleaned-up version in case anyone
is interested...

-Steve
-- 
Steve Wampler -- [EMAIL PROTECTED] 
The gods that smiled on your birth are now laughing out loud.


-----------------------------------------
This e-mail and any attachments may contain CONFIDENTIAL information,
including PROTECTED HEALTH INFORMATION. If you are not the intended
recipient, any use or disclosure of this information is STRICTLY
PROHIBITED; you are requested to delete this e-mail and any
attachments, notify the sender immediately, and notify the LabCorp
Privacy Officer at [EMAIL PROTECTED] or call (877) 23-HIPAA /
(877) 234-4722.



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Unicon-group mailing list
Unicon-group@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to