Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归如何跟踪答案?_Python_Recursion - Fatal编程技术网

Python 递归如何跟踪答案?

Python 递归如何跟踪答案?,python,recursion,Python,Recursion,我正在Hackerrank上尝试这个挑战: 有两种方法可以解决此问题: 递归 动态规划 我的问题:我不理解递归解决方案的一部分。我不明白当“答案”一直设置为零时,它是如何跟踪的 样本输入: cur 0 cur 0 cur 0 cur 0 the 0 cur 0 the 0 the 0 cur 0 cur 0 the 0 the 0 cur 0 the 0 cur 0 the 0 the 0 cur 0 cur 0 a match: 4 9 16 the 1 the 1 cur 0 the 1

我正在Hackerrank上尝试这个挑战:

有两种方法可以解决此问题:

  • 递归
  • 动态规划
我的问题:我不理解递归解决方案的一部分。我不明白当“答案”一直设置为零时,它是如何跟踪的

样本输入:

cur 0
cur 0
cur 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
the 0
the 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
a match:
4 9 16 the 1
the 1
cur 0
the 1
a match:
4 25 the 2
the 2
cur 0
cur 0
the 0
the 2
cur 0
the 2
cur 0
the 2
import math
import os
import random
import re
import sys

def powerSum(x, n, value):
    s = sum (v**n for v in value)
    if s == x:
        print "a match:"
        for v in value:
            print v**n,
        return 1
    else:
        v = value[-1] + 1 if value else 1
        answer = 0
        print "cur", answer
        while s + v**n <= x:
            answer += powerSum(x, n, value+[v])
            v += 1
            print "the", answer
    return answer


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x = int(raw_input())

    n = int(raw_input())

    value = []

    result = powerSum(x, n, value)

    fptr.write(str(result) + '\n')

    fptr.close()
二十九

二,

正确答案:2

样本输出:

cur 0
cur 0
cur 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
the 0
the 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
a match:
4 9 16 the 1
the 1
cur 0
the 1
a match:
4 25 the 2
the 2
cur 0
cur 0
the 0
the 2
cur 0
the 2
cur 0
the 2
import math
import os
import random
import re
import sys

def powerSum(x, n, value):
    s = sum (v**n for v in value)
    if s == x:
        print "a match:"
        for v in value:
            print v**n,
        return 1
    else:
        v = value[-1] + 1 if value else 1
        answer = 0
        print "cur", answer
        while s + v**n <= x:
            answer += powerSum(x, n, value+[v])
            v += 1
            print "the", answer
    return answer


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x = int(raw_input())

    n = int(raw_input())

    value = []

    result = powerSum(x, n, value)

    fptr.write(str(result) + '\n')

    fptr.close()
代码:

cur 0
cur 0
cur 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
the 0
the 0
cur 0
the 0
cur 0
the 0
the 0
cur 0
cur 0
a match:
4 9 16 the 1
the 1
cur 0
the 1
a match:
4 25 the 2
the 2
cur 0
cur 0
the 0
the 2
cur 0
the 2
cur 0
the 2
import math
import os
import random
import re
import sys

def powerSum(x, n, value):
    s = sum (v**n for v in value)
    if s == x:
        print "a match:"
        for v in value:
            print v**n,
        return 1
    else:
        v = value[-1] + 1 if value else 1
        answer = 0
        print "cur", answer
        while s + v**n <= x:
            answer += powerSum(x, n, value+[v])
            v += 1
            print "the", answer
    return answer


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x = int(raw_input())

    n = int(raw_input())

    value = []

    result = powerSum(x, n, value)

    fptr.write(str(result) + '\n')

    fptr.close()
导入数学
导入操作系统
随机输入
进口稀土
导入系统
def功率(x,n,值):
s=总和(v**n表示v的值)
如果s==x:
打印“匹配项:”
对于v值:
打印v**n,
返回1
其他:
v=值[-1]+1,如果值为1
答案=0
打印“cur”,回答

而您在问题中提供的代码以稍微复杂的方式实现了递归。仅此而已。考虑一个不同版本的同一代码:

def powerSum(x, n, value):

    answer = 0

    s = sum(v**n for v in value)
    # base case
    if s == x:
        answer = 1
    # recursive case
    else:
        v = value[-1] + 1 if value else 1
        while s + v**n <= x:
            answer += powerSum(x, n, value+[v])
            v += 1

    return answer
def powerSum(x,n,值):
答案=0
s=总和(v**n表示v的值)
#基本情况
如果s==x:
答案=1
#递归案例
其他:
v=值[-1]+1,如果值为1

每次运行
powerSum
时,s+v**n
answer
未分配
0
。每当你从
powerSum
内部调用
powerSum
时,你基本上是在创建一个新的、独立的
答案
,它与原始的
答案
没有任何关联。哇,我怎么能对这样的开始感到惊讶呢。这适用于所有递归吗?你能给我指一下参考资料吗?我可以在那里读到更多关于这方面的资料吗?这不仅仅是递归,任何涉及范围变化的东西。递归函数的工作原理与非递归函数完全相同–这就像有两个完全不同的函数一样,都有一个称为“answer”的局部变量,并且从另一个函数调用一个。