猿问

python java转换的受限硬币找零问题

我有一个 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)


交互式爱情
浏览 80回答 1
1回答

12345678_0001

将该行替换为int[] r = new int[n+1];r[0] = 1;for (int i = 1; i < r.length; i++)&nbsp;&nbsp; &nbsp; r[i] = 0;
随时随地看视频慕课网APP

相关分类

Python
我要回答