【发布时间】:2019-07-08 02:46:21
【问题描述】:
我在将以下代码从使用循环转换为仅使用递归时遇到了一些麻烦。
//longestHailstoneStartValue also cycles through each of the sequences from 1
//to 'n' and after it is determined what the largest length is, it finds which
//number corresponds with the longest length function.
//
//Sample input: '8'
//Sample output: '7'
//'7' is from the sequence starting with 7.
int longestHailstoneStartValue(int n)
{
int u=1, w=0, z=0;
while(n>=u)
{
if(w<lengthHailstone(u))
{
w=lengthHailstone(u);
z=u;
}
u++;
}
return z;
}
我必须将其转换为递归,并且无论如何都要采用任何未使用/具有新值存储在其中的额外变量。
【问题讨论】: