Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
java.lang.StackOverflowerr用于5位或以上的值_Java_Stack Overflow - Fatal编程技术网

java.lang.StackOverflowerr用于5位或以上的值

java.lang.StackOverflowerr用于5位或以上的值,java,stack-overflow,Java,Stack Overflow,下面是我的代码应该如何工作。首先,它在变量t中使用了大量测试用例。现在文本案例的数量可以达到100000个。然后它接受两个输入,一个用于行,另一个用于列。输入样本- 3 2 3 6 7 10 10 应该发生的是- We have the following matrix 1 0 0 0 0 0 ... 2 2 0 0 0 0 ... 3 3 3 0 0 0 ... 4 4 4 4 0 0 ... 5 5 5 5 5 0 ... 6 6 6 6 6 6 ... and so on ...

下面是我的代码应该如何工作。首先,它在变量t中使用了大量测试用例。现在文本案例的数量可以达到100000个。然后它接受两个输入,一个用于行,另一个用于列。输入样本-

3
2 3
6 7
10 10
应该发生的是-

We have the following matrix 
1 0 0 0 0 0 ...
2 2 0 0 0 0 ...
3 3 3 0 0 0 ...
4 4 4 4 0 0 ... 
5 5 5 5 5 0 ... 
6 6 6 6 6 6 ...
and so on ... 
The matrix is created as follows, first row contains one 1 and rest 0's, second row
contains 2 twos and rest zeros, third row contains 3 threes and so on.

Given R and C, calculate the count of even and odd numbers in sub matrix[R,C].
0 is neither odd nor even and 1 based indexing is used i.e. matrix[1,1]=1

Output
For each test case print count of even and odd numbers in sub matrix[R,C].
输出样本-

2 1
12 9
30 25
现在,对于我的代码中的小数字,一切都很好。行和列的最大值应分别为100000。这就是问题所在。对于行或列中的25000输入值,代码工作正常,但如果我为其中任何一个输入30000或更大的值,就会得到StackOverflowerError

这是我的完整代码-

import java.util.Scanner;

public class TestClass {
public static void main(String args[]) throws Exception {

    Scanner input = new Scanner(System.in);
    int t = input.nextInt();
    int[][] storage = new int[t][2];

    for (int i = 0; i < t; i++) {
        for (int j = 0; j < 2; j++) {
            int x = input.nextInt();
            storage[i][j] = x;

        }
    }
    TestClass test = new TestClass();
    for (int i = 0; i < storage.length; i++) {
        int a = storage[i][0];
        int b = storage[i][1];
        test.theMethod(a, b);
    }
    input.close();
}

public void theMethod(int x, int y) {
    int d = x - y;
    int first = 0;
    int second = 0;
    int[][] resultarray = new int[1][2];
    if (x % 2 == 0) {
        if (x >= 1) {
            if (y >= x) {
                first = number(x);
                second = number(x - 1) + 1;
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            } else if (y < x) {
                first = (number(x) - number(d)) - 1;
                second = (number(x - 1) - number(d - 1)) + 1;
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            }
        }
    } else if (x % 2 != 0) {
        if (x >= 1) {
            if (y >= x) {
                first = number(x) + 1;
                second = number(x - 1);
                resultarray[0][0] = first;
                resultarray[0][1] = second;
            } else if (y < x) {
                first = (number(x) - number(d));
                second = number(x - 1) - number(d - 1);
                resultarray[0][0] = second;
                resultarray[0][1] = first;
            }
        }
    }
    for (int i = 0; i < resultarray.length; i++) {
        for (int j = 0; j < resultarray[i].length; j++) {
            System.out.print(resultarray[i][j]);
            System.out.print(" ");
        }
        System.out.println();
    }

}

public int number(int x) {
    if (x == 1 || x == 0) {
        x = 0;
    } else if (x > 1) {
        x = x + number(x - 2);      //*****this is the error line
    }
    return x;
}
}

现在,对于大于25000或30000左右的值,将显示此错误。我想提到的一件事是,在这个程序中,我曾尝试对每个int值使用long,但对于更低的输入值,我仍然会得到同样的错误。有人知道我做错了什么吗?

你需要停止使用递归。由于
number
方法中的递归调用,堆栈空间不足。您可以修改该方法以使用循环,而不是对其自身的递归调用。这将防止堆栈空间耗尽。

堆栈跟踪非常清楚地告诉您是什么导致堆栈溢出,这就是
number()
方法

简单看一下,对于每个数字
n
,它将有大约
n/2
级别的递归调用
number()
。如果超过30000,则调用堆栈中的级别将达到15000,这相当多

您的
number()
实际上可以使用以下简单循环重写:

int number(int x) {
    int result = 0;

    while (x > 1) {
       result += x;
       x -= 2;
    }
    return result;
}

您的递归太远,并且在堆栈上使用了太多内存。有关更多信息,请参阅。
int number(int x) {
    int result = 0;

    while (x > 1) {
       result += x;
       x -= 2;
    }
    return result;
}