gpt4 book ai didi

对于 5 位或更多数字的值,出现 java.lang.StackOverFlowError

转载 作者:行者123 更新时间:2023-12-02 05:25:59 25 4
gpt4 key购买 nike

这是我的代码应该如何工作。首先,它需要变量 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 或更大的值,则会出现 stackoverflowerror。

这是我的完整代码 -

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;
}
}

错误堆栈跟踪 -

Exception in thread "main" java.lang.StackOverflowError
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)
at TestClass.number(TestClass.java:74)

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

最佳答案

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

关于对于 5 位或更多数字的值,出现 java.lang.StackOverFlowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com