projecteuler---->problem=31----Coin sums 无限背包计算可能存在的次数

Problem 31

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).

It is possible to make £2 in the following way:

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p

How many different ways can £2 be made using any number of coins?

import time
def cal(sum,count,value):
	if sum == 200:
		count += 1
		return
	if sum > 200 :
		return
	for i in range(0,8):
		sum += value[i]
		cal(sum,count,value)
		sum -= value[i]

begin = time.time()
num={1,2,5,10,20,50,100,200};
num=list(num)
count=0
cal(0,count,num)
end = time.time()
print count
print end-begin

第一种办法,深搜超时

#
# 解析:每次从列表中取出最大的coin值,得到取最大值个数的上限
#      个数叠加,接下来的用较小的数凑。
#
#
def cal(value, coins):
	if value == 0 or len(coins)==1 :
		return 1
	else:
		coins = sorted(coins)
		largest = coins[-1]
		uses = value / largest
		total = 0
		for i in range(uses+1):
			total += cal(value-largest*i, coins[:-1])
		return total

print cal(200, [1,2,5,10,20,50,100,200])

第二种 贪心凑极值 AC

projecteuler---->problem=31----Coin sums 无限背包计算可能存在的次数

时间: 2024-08-04 21:48:32

projecteuler---->problem=31----Coin sums 无限背包计算可能存在的次数的相关文章

Project Euler:Problem 31 Coin sums

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

RQNOJ 671 纯洁的买卖:无限背包

题目链接:https://www.rqnoj.cn/problem/671 题意: ALEJ要通过倒卖东西来赚钱. 现在他有m元经费. 有n种物品供他选择,每种物品数量无限. 第i件物品的买入价为c[i],卖出价为r[i],每卖出一件物品i后,要交c[i]的税. 问:一次买卖之后,经费最多有多少. 题解: 注:(1)"买"和"卖"是有顺序的. 也就是说,收购一件物品所得到的"未来利润"并不能当作现在的经费来用. (2)"缴税"

欧拉计划(python) problem 31

Coin sums Problem 31 In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1×£1 + 1×50p + 2×

CodeForces 283C Coin Troubles 分析+背包思想

很灵活的题目,题意简单,看到又是钱币问题,类似于那种给了一定数目T,有n种钱币,每种的价值,让你组合成总价值为T的方案数,但是加了一些限制条件,那就是某些种类钱币数量必须大于另一些种类的,加了个限制条件 我就脑残了,唉智商看来是真不够啊 ,后来看了别人的分析 倘若种类a的钱币数量必须要大于种类b的数量,那么如果我要 去 m张b种类的钱币,其实同时也是相当于已经取了m张a种类的,因为a必须大于b的嘛,所以我们可以通过这样来修改题目给的钱币的价值,若a种类数量必须大于b种类数量,且a种类价值为A,b

uva674 - Coin Change(完全背包)

题目:uva674 - Coin Change(完全背包) 题目大意:给1 5 10 25 50 这5中面值的硬币,然后给出N,问用这些钱组成N的不同方式数目.1 5 和 5 1 表示同一中,顺序不同算相同. 解题思路:完全背包. 状态方程:dp[j] += dp[ j - v[i]]: 代码: #include <cstdio> #include <cstring> const int N = 5; const int maxn = 8000; typedef long long

01背包与物品无限背包

一.01背包问题 01背包是在M件物品取出若干件放在空间为W的背包里,每件物品的体积为C1,C2,…,Cn,与之相对应的价值为W1,W2,…,Wn.求解将那些物品装入背包可使总价值最大. 动态规划: 1) 子问题定义:F[i][j]表示前i件物品中选取若干件物品放入剩余空间为j的背包中所能得到的最大价值. 2) 根据第i件物品放或不放进行决策 其中F[i-1][j]表示前i-1件物品中选取若干件物品放入剩余空间为j的背包中所能得到的最大价值: 而F[i-1][j-C[i]]+W[i]表示前i-1

HDU1686 计算模式串匹配的次数

题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于1000000,w长度小于10000,计算W匹配到T中成功的次数: 这题很明显要用KMP算法,不然很容易超时,但在使用kmp算法时也要注意,我第一次将匹配成功的位置得到后,循环进入kmp算法,从前一个匹配到的位置开始算起,但是超时了.后来问完学长之后,清楚了,没必要循环进入kmp,直接一次可以搞定,每次模式串匹配到末尾时,都将计数+1,然后根据next[m],重新得到j继续与原串进行匹配直到进行到原串

python之Counter类:计算序列中出现次数最多的元素

Counter类:计算序列中出现次数最多的元素 1 from collections import Counter 2 3 c = Counter('abcdefaddffccef') 4 print('完整的Counter对象:', c) 5 6 a_times = c['a'] 7 print('元素a出现的次数:', a_times) 8 9 c_most = c.most_common(3) 10 print('出现次数最多的三个元素:', c_most) 11 12 times_dic