有多组数据,对于每组数据,首先是要求凑成的邮票总值M,M<100。然后是一个数N,N〈20,表示有N张邮票。接下来是N个正整数,分别表示这N张邮票的面值,且以升序排列。
对于每组数据,能够凑成总值M的最少邮票张数。若无解,输出0。
10 5 1 3 3 3 4
3
def getComNum(nowIndex,nowSum,nowStampNum): if nowIndex >= n: return if stamps[nowIndex] + nowSum > m: return elif stamps[nowIndex] + nowSum == m: global result result = min(result,nowStampNum+1) getComNum(nowIndex+1,nowSum,nowStampNum) else: getComNum(nowIndex+1,nowSum+stamps[nowIndex],nowStampNum+1) getComNum(nowIndex + 1, nowSum,nowStampNum) while True: try: m = int(input()) n = int(input()) stamps = list(map(int,input().split())) result = float('inf') getComNum(0,0,0) if result == float('inf'): result = 0 print(result) except Exception: break