我有一个 python 函数它计算可能的硬币变化量。每个硬币的可用性有限。我试图理解它以将其转换为java但我在该行失败了r=有人可以解释这一行发生了什么吗?# cs is a list of pairs (c, k) where there's k# coins of value c.def limited_coins(cs, n): r = [1] + [0] * n for c, k in cs: # rs[i] will contain the sum r[i] + r[i-c] + r[i-2c] + ... rs = r[:] for i in xrange(c, n+1): rs[i] += rs[i-c] # This line effectively performs: # r'[i] = sum(r[i-j*c] for j=0...k) # but using rs[] so that the computation is O(1) # and in place. r[i] += rs[i-c] - (0 if i<c*(k+1) else rs[i-c*(k+1)]) return r[n]for n in xrange(50): print n, limited_coins([(1, 3), (2, 2), (5, 3), (10, 2)], n)
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
将该行替换为
int[] r = new int[n+1];
r[0] = 1;
for (int i = 1; i < r.length; i++)
r[i] = 0;
添加回答
举报
0/150
提交
取消