Java 开发递归程序计算答案
我需要开发一个递归程序,当调用f3(1,I)时,可以计算结果1/I+2/(I-1)+3/(I-2)+…+(I-1)/2+I/1Java 开发递归程序计算答案,java,recursion,Java,Recursion,我需要开发一个递归程序,当调用f3(1,I)时,可以计算结果1/I+2/(I-1)+3/(I-2)+…+(I-1)/2+I/1 public class Project4f3 { public static int f3(int x, int y) // I know that it has to have a stop termination and i think that is when x==y, but im not sure if (x==y) return ?? else
public class Project4f3
{
public static int f3(int x, int y)
// I know that it has to have a stop termination and i think that is when x==y, but im not sure
if (x==y) return ??
else return f3(x+1,y-1)
// don't think this is right but this is where I am at right now
}
public static void main(String[] args)
{
System.out.print(f3(1,i));
}
}
要开发递归算法,您需要考虑两件事:
n==1
,您可以直接计算答案n-1
的答案,您将如何使用它来计算n
的答案(我应该指出,我在这里使用的
n
不一定是你在等式中使用的I
。使用等式中的项数作为n
,或者可能是中间项两边的元素数,可能更有意义。递归问题解决的创造性部分,通常也是最困难的是提出正确的问题表示。)Nevermine,我发现了它。如果你不确定,你为什么不试试看?你已经问过这个问题了,然后删除它。请删除它并编辑它,而不是创建新的帖子。你想总结所有的迭代吗?是的……但是我不知道1和I是如何在问题中间切换的。