[Python] 给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法 →→→→→进入此内容的聊天室

来自 , 2020-12-20, 写在 Python, 查看 174 次.
URL http://www.code666.cn/view/5a7b238b
  1. #!/usr/bin/python
  2. #+
  3. # This script computes the number of different ways that combinations
  4. # of a given set of coin denominations can add up to a specified amount.
  5. #
  6. # Call this script as follows:
  7. #
  8. #     make_change --coins=denomination,denomination... [--total] amount ...
  9. #
  10. # where amount is one or more amounts (in integer cents) to compute change
  11. # for, denomination is the list of available coin denominations, and
  12. # --total means to only list the total number of ways to make change,
  13. # not show the details of the combinations themselves.
  14. #
  15. # The details of the combinations are displayed on standard output as
  16. # follows:
  17. #
  18. #     amount => [n1 * denom1, n2 * denom2 ... ]
  19. #
  20. # where the n are the integer numbers of each coin, and the denom are
  21. # the corresponding coin denomination, to make the total add up to amount.
  22. #-
  23.  
  24. import sys
  25. import getopt
  26.  
  27. def CoinsCombination(Coins, Amount) :
  28.     """generator function which yields all possible combinations of
  29.    Coins adding up to Amount. Assumes Coins is sorted by increasing
  30.    value."""
  31.     if len(Coins) == 0 :
  32.         if Amount == 0 :
  33.             yield ()
  34.         else :
  35.             return
  36.         #end if
  37.     else :
  38.         Coeff = 0
  39.         while True :
  40.             for Combi in CoinsCombination(Coins[1:], Amount) :
  41.                 yield (Coeff,) + Combi
  42.             #end for
  43.             if Coins[0] > Amount :
  44.                 break
  45.             Coeff += 1
  46.             Amount -= Coins[0]
  47.         #end while
  48.     #end if
  49. #end CoinsCombination
  50.  
  51. (Opts, Args) = getopt.getopt \
  52.   (
  53.     sys.argv[1:],
  54.     "",
  55.     ["coins=", "total"]
  56.   )
  57. Coins = None
  58. TotalOnly = False
  59. for Keyword, Value in Opts :
  60.     if Keyword == "--coins" :
  61.         Coins = []
  62.         for Denom in Value.split(",") :
  63.             Coin = int(Denom)
  64.             if Coin <= 0 :
  65.                 raise getopt.error("bad --coins denomination \"%s\"" % Denom)
  66.             #end if
  67.             Coins.append(Coin)
  68.         #end for
  69.         if len(Coins) == 0 :
  70.             raise getopt.error("empty --coins specification")
  71.         #end if
  72.         Coins.sort()
  73.     elif Keyword == "--total" :
  74.         TotalOnly = True
  75.     #end if
  76. #end for
  77. if Coins == None :
  78.     raise getopt.error("missing --coins specification")
  79. #end if
  80. if len(Args) == 0 :
  81.     raise getopt.error("missing amounts")
  82. #end if
  83. for Amount in Args :
  84.     Amount = int(Amount)
  85.     NrWays = 0
  86.     for Combi in CoinsCombination(Coins, Amount) :
  87.         NrWays += 1
  88.         if not TotalOnly :
  89.             print "%u => [%s]" % \
  90.                     (Amount,
  91.                      ", ".join(str(a) + " * " + str(b) for a, b in zip(Combi, Coins)))
  92.         #end if
  93.     #end for
  94.     print "%u nr ways = %u" % (Amount, NrWays)
  95. #end for
  96. #//python/4606

回复 "给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法"

这儿你可以回复上面这条便签

captcha