题目:
思路分析:
代码:
import math def f(coins, n): # 最少找零数 le = len(coins) FF = [0 for _ in range(n+1)] for i in coins: if i <= n: FF[i] = 1 for i in range(1, n+1): ll = [i for _ in range(le)] result = [math.inf if (x - y) < 0 else (x - y) for x, y in zip(ll, coins)] values = [] for j in result: if 0 <= j < len(FF): values.append(FF[j]) else: values.append(math.inf) m = min(values) + 1 FF[i] = m # 找零选择 IND = [] while n > 0: uu = [n for _ in range(le)] res = [math.inf if (x - y) < 0 else (x - y) for x, y in zip(uu, coins)] val = [] for j in res: if 0 <= j < len(FF): val.append(FF[j]) else: val.append(math.inf) if min(val) >= 0: IND.append(coins[val.index(min(val))]) n -= coins[val.index(min(val))] else: break return FF, FF[-1], IND l = list(map(int, input().split())) n = int(input()) FF, re, IND = f(l, n) print(f"最少找零数为:{re}") print(FF) print("硬币选择为:") print(IND)