- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码应该如何工作。首先,它需要变量 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/
我有一个使用 play scala 2.0 开发的项目,它工作正常,我需要将版本升级到 2.3.8。所以我通过此链接迁移了我的应用程序版本 https://www.playframework.com/
为此我需要一些帮助。 首先我有一个 BinarySearchTree 类 import java.util.ArrayList; import java.util.List; public class
我正在尝试使用递归方法计算字母“e”在给定字符串中出现的次数。我的测试字符串是 请数我的 e!。这是到目前为止的代码: public static int showE(String s, int co
您将如何调整这个简单的递归示例,以便进行尾调用优化(而不是 StackOverflowError)? count 0 = 0 count n = succ (count (pred n)) count
我根据自身定义流(递归定义)。当试图访问流的第二个元素时,StackOverflowError被抛出。来自Scala控制台的代码: scala> val s1 = Stream.iterate(1)(
我在 Java 中有一个 StackOverflowError,它没有告诉我我自己的代码中的任何一行,堆栈跟踪的相关部分是: java.lang.StringBuilder.append(String
这个隐式 val 如何导致 StackOverFlowError? (削减我的原始代码,仍然导致错误) object Complicit { // a class with name, defau
在 Groovy Console我有这个: import groovy.util.* import org.codehaus.groovy.runtime.* def gse = new Groovy
为什么此代码片段执行会导致 StackOverflowError: lazy val primes: Stream[Int] = 2 #:: Stream.from(3, 2) filter { pc
(reduce concat (repeat 10000 [])) 我知道展平可能是执行此操作的更好方法,但我仍然很好奇为什么这会导致错误。 最佳答案 因为concat产生一个惰性序列。 所以,当你打
当我使用 (avg-bids 4000 10 5) 调用以下 Clojure 代码时,会导致 java.lang.StackOverflowError。我试图找出原因,因为 sum-bids 是作为尾
我在运行递归程序时遇到了 Java StackOverFlowError。程序正确,需要实现递归。我尝试使用命令查找当前堆栈大小 java -XX:+PrintFlagsFinal -vers
美好的一天!运行快速排序算法时,我收到 StackOverflowError 错误。当数组中的元素 > 50 000 时,会发生此错误。 我的代码如下: public void recQuickSor
我正在删除一个 Android 应用程序,其中有一个无限重复的动画,导致 StackOverflowError。当同一对象上的另一个动画开始时,它会执行此操作。 private fun pulse()
我创建了一个公共(public)类PermissionManager来管理来自一个地方的所有权限,通常它工作正常,但上传后它显示崩溃分析的错误报告我无法重现,详细信息是下面提到 Fatal Excep
我得到了一组称为“字典”的字符串,存储为字段,代表单词字典。 我要编写一个方法,它接受一个字符串参数(“短语”)并返回一个包含字典集中所有单词的集合,这些单词可以通过重新排列给定短语中的字符来实现。基
我正在尝试生成一个相对较小(1296 个元素)的向量列表,本质上枚举从 [0 0 0 0] 到 [5 5 5 5] 的 4 个基数 6 数字 [0 0 0 0], [1 0 0 0] ... [5 0
我正在尝试用java编写二进制插入排序。 public static int binarySearch(double[] a, int max, int min, double k) {
我目前正在 Clojure 中实现欧拉项目问题之一的解决方案,即埃拉托斯特尼筛法 ( https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes )。这是我
我遇到了与错误递归和 StackOverflowError 相关的编程问题。我在一个单独的线程中处理了这个案例: public void subscribe(final String channel)
我是一名优秀的程序员,十分优秀!