2014-09-24 31 views
0

這是我的代碼應該如何工作。首先在變量t中需要一些測試用例。現在,文本案例的數量可以是任何達到100000的文本。然後,它需要兩個輸入,一個輸入一行,另一個輸入一列。輸入樣品 -java.lang.StackOverFlowError的值爲5位或更多

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]. 

輸出的Sample-

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,但我仍然得到相同的錯誤,出乎意料地甚至是更低的輸入值。任何人都知道我做錯了什麼?

+1

你是遞歸的太遠,使用棧上太多內存。看看http://stackoverflow.com/questions/4734108/what-is-the-maximum-depth-of-the-java-call-stack獲取更多信息。 – mkobit 2014-09-24 01:36:53

回答

2

您需要停止使用遞歸。由於number方法中的遞歸調用,您的堆棧空間不足。您可以修改該方法以使用循環而不是遞歸調用自身。這樣可以防止堆棧空間用完。

0

堆棧跟蹤非常清楚導致堆棧溢出的原因,即number()方法。

只是簡單的看一下,對於每個號碼n,它都會有大約n/2的遞歸調用水平number()。如果你傳遞30000,那麼在調用堆棧中將達到15000個級別,這是相當多的。

number()能實際使用一個簡單的循環像這樣被改寫:

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

    while (x > 1) { 
     result += x; 
     x -= 2; 
    } 
    return result; 
} 
相關問題