我在迭代和递归中编写了一些函数的代码,但是在测试方法时我遇到了递归问题。我试图解决它们,但我总是遇到其他问题,就像StackOverflowError一样。有人能告诉我如何解决它们吗?
public final class Functions {
private Functions() {
}
public static long f2R(int n) throws IllegalArgumentException {
if (n < 0)
throw new IllegalArgumentException();
if (n == 0) {
return f2R_helper(n, 1, 2);
}
return (long) (Math.pow(2, n) + f2R(n - 1));
}
private static long f2R_helper(int n, long s, long p) {
return (n == 0) ? s : f2R_helper(n - 1, s + p, p * 2);
}
public static long f2I(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException();
}
long result = 0;
for (int i = 0; i <= n; i++) {
result = result + (long) Math.pow(2, i);
}
return result;
}
public static long f3R(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException();
}
if (n == 0) {
return 0;
}
return f3Rhelper(n, n) + f3Rhelper(n - 1, n - 1);
}
private static long f3Rhelper(long k, long i) {
if (k == 0) {
return 0;
}
return (k * i) + f3Rhelper(k, i - 1);
}
public static long f3I(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException();
}
long result = 0;
long sum = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= i; j++) {
result = result + (i * j);
}
sum = sum + result;
result = 0;
}
return sum;
}
public static long f4R(int n) throws IllegalArgumentException {
if (n < 0)
throw new IllegalArgumentException();
if (n == 0) {
return 0;
}
return f4R(n - 1) + f4R_helper(n);
}
private static long f4R_helper(int n) {
if (n == 0) {
return 0;
}
return n * (n + 1) + f4R_helper(n - 1);
}
public static long f4I(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException();
}
long result = 0;
for (int i = 0; i <= n; i++) {
result = result+ i * (i + 1);
}
return result;
}
}
public class FunctionsTest {
@Test
public void test() {
long[] check1 = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};
long[] check2 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
for (int i=1; i<check1.length; i++) {
check2[i] = check2[i-1] + check1[i];
}
for (int i=0; i<check2.length; i++) {
assertEquals(check2[i], f2R(i));
}
for (int i=0; i<100; i++) {
assertEquals(f2R(i), f2I(i));
}
long[] check3a = {
0,
1*0 + 1*1,
2*0 + 2*1 + 2*2,
3*0 + 3*1 + 3*2 + 3*3,
4*0 + 4*1 + 4*2 + 4*3 + 4*4,
5*0 + 5*1 + 5*2 + 5*3 + 5*4 + 5*5,
6*0 + 6*1 + 6*2 + 6*3 + 6*4 + 6*5 + 6*6,
7*0 + 7*1 + 7*2 + 7*3 + 7*4 + 7*5 + 7*6 + 7*7,
};
long[] check3 = {
check3a[0],
check3a[0] + check3a[1],
check3a[0] + check3a[1] + check3a[2],
check3a[0] + check3a[1] + check3a[2] + check3a[3],
check3a[0] + check3a[1] + check3a[2] + check3a[3] + check3a[4],
check3a[0] + check3a[1] + check3a[2] + check3a[3] + check3a[4] + check3a[5],
check3a[0] + check3a[1] + check3a[2] + check3a[3] + check3a[4] + check3a[5] + check3a[6],
check3a[0] + check3a[1] + check3a[2] + check3a[3] + check3a[4] + check3a[5] + check3a[6] + check3a[7]
};
for (int i=0; i<8; i++) {
assertEquals(f3R(i), check3[i]);
}
for (int i : new int[]{81,3,9,4,6,7,0,12,78,34} ) {
assertEquals(f3I(i), f3R(i));
}
long[] check4a = {0, 2, 6, 12, 20, 30, 42, 56, 72};
long[] check4 = {
check4a[0],
check4a[0]+check4a[1],
check4a[0]+check4a[1]+check4a[2],
check4a[0]+check4a[1]+check4a[2]+c…
check4a[0]+check4a[1]+check4a[2]+c…
check4a[0]+check4a[1]+check4a[2]+c…
check4a[0]+check4a[1]+check4a[2]+c…
check4a[0]+check4a[1]+check4a[2]+c…
check4a[0]+check4a[1]+check4a[2]+c…
};
for (int i=0; i<check4.length; i++) {
assertEquals(f4R(i), check4[i]);
}
}
}
答案 0 :(得分:1)
StackOverflowError通常意味着您的递归不会结束
添加一些终止条件。例如,如果您计算
通过递归函数的阶乘,然后你的终止
条件通常是N = 0或N = 1的情况