<p>要开始:</p>
<pre><code>0 < pwr < 6
</code></pre>
<p>…相当于:</p>
^{pr2}$
<p>这种<code>0 < pwr < 6</code>符号在数学和一些编程语言(包括Python)中非常常见。有关详细信息,请参见维基百科上的<a href="http://en.wikipedia.org/wiki/Inequality_%28mathematics%29#Chained_notation" rel="nofollow">Chained notation</a>。在</p>
<p>因此,对于每一个这样的幂,尝试找到一个<code>root</code>,这样<code>root**pwr</code>就是用户的输入值。在</p>
<p>使用<a href="http://docs.python.org/3/library/math.html#math.log" rel="nofollow">^{<cd4>}</a>有一个快速的方法来实现这一点,但是如果您不知道对数是什么,就不用担心了。相反,您可以尝试所有可能的<code>root</code>。如果任何<code>root</code>太大,那么所有较大的<code>root</code>也将太大。在</p>
<p>除了一个问题:如果<code>user_input</code>是负数呢?所以,你需要找到一种方法来计算所有的正数和负数,直到你找到一个绝对值太大的数值。在</p>
<p>所以,用伪代码写一个简图:</p>
<pre><code>def find_root_pwr(user_input):
for every pwr such that 0 < pwr < 6:
for every possible root:
test_value = root**pwr
if it's a match:
return root, pwr
elif abs(test_value) is too big:
break # don't need to check larger roots
else:
# We tried all 5 powers, and no match
raise ValueError('No answer for {}'.format(user_input))
</code></pre>
<p>为了测试它:</p>
<pre><code>user_input = int(input()) # raw_input if you're on Python 2.x
root, pwr = find_root_pwr(user_input)
print('{} ** {} = {}'.format(root, pwr, user_input))
</code></pre>
<p>有关如何编写循环的提示,请参见<a href="http://docs.python.org/3/library/functions.html#func-range" rel="nofollow">^{<cd9>}</a>和<a href="http://docs.python.org/3/library/itertools.html#itertools.count" rel="nofollow">^{<cd10>}</a>。你应该能够从这里填写细节,或者至少可以有一个更具体的后续问题。在</p>