![](/img/trans.png)
[英]Why do I get java.lang.StackOverflowError when using Flood Fill algorithm?
[英]why do i get java.lang.StackOverflowError here?
_以下問題的以下程序給出了一系列異常Exception in thread "main" java.lang.StackOverflowError at testing_package.Compute.factorial(Compute.java:105)
我不明白為什么會收到此錯誤。
問題 :N個男孩和M個女孩正在從劇院學習表演技巧。 要表演戲劇,他們需要組成一組P演員,其中不少於4個男孩,且不少於1個女孩。 劇院要求您編寫一個程序,告訴他們組的形成方式的數量。 注意:合成應該是唯一的,而不是合成的順序。
import java.io.*;
class Compute {
private static int NFact;
private static int N_Minus_R_Fact;
private static int RFact;
private static int fact=0;
public static int readTheStrengthOfGroup() {
int strengthOfGroup=0;
try {
System.out.println("Enter the strength of group : ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String read = reader.readLine();
strengthOfGroup = Integer.parseInt(read);
} catch(Exception exc) {
System.out.println(exc);
}
return strengthOfGroup;
}
public static int readTheNumberOfBoys() {
int boysToParticipate=0;
try {
System.out.println("Enter the number of boys to participate in the play : ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String read = reader.readLine();
boysToParticipate = Integer.parseInt(read);
} catch(Exception exc) {
System.out.println(exc);
}
return boysToParticipate;
}
public static int readTheNumberOfGirls() {
int girlsToParticipate=0;
try {
System.out.println("Enter the number of girls to participate in the play : ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String read = reader.readLine();
girlsToParticipate = Integer.parseInt(read);
} catch(Exception exc) {
System.out.println(exc);
}
return girlsToParticipate;
}
public static int compute(int strengthOfGroup , int boysToParticipate , int girlsToParticipate) {
if( boysToParticipate < 4 || girlsToParticipate < 1) {
return 0;
} else {
/* P >= 5
* N : Boys
* M : Girls
* result = M+N C P - { (N C 0)(M C P)+(N C 1)(M C P-1)+(N C 2)(M C P-2)+(N C 3)(M C P-3)+(N C P)(M C 0) }
*/
int resultP_2 = 0;
int totalNumberOfParticipants = boysToParticipate + girlsToParticipate;
int totalNumberOfParticipants_C_strengthOfGroup = computeFactorial(totalNumberOfParticipants , strengthOfGroup);
for( int i = 0 ; i <= 4 ; i++ ) {
if( i == 4 ) {
resultP_2 = resultP_2 + (computeFactorial(boysToParticipate,strengthOfGroup) * computeFactorial(girlsToParticipate,0));
}else {
resultP_2 = resultP_2 + (computeFactorial(boysToParticipate,i) * computeFactorial(girlsToParticipate,strengthOfGroup));
strengthOfGroup--;}
}
int result = totalNumberOfParticipants_C_strengthOfGroup - resultP_2;
return result;
}
}
public static int computeFactorial(int N , int R) {
if(R > N) {
throw new RuntimeException("Invalid Parameters");
} else {
/* int NFact;
int N_Minus_R_Fact;
int RFact; */
NFact = factorial(N);
N_Minus_R_Fact = factorial(N-R);
RFact = factorial(R);
return( NFact / ( N_Minus_R_Fact-RFact ) );
}
}
public static int factorial(int num) {
if( num == 1 ) {
return 1;
} else {
fact = num * factorial(num-1); // LINE 105
return fact;
}
}
public static void main(String args[]) {
int strengthOfGroup = readTheStrengthOfGroup();
int boysToParticipate = readTheNumberOfBoys();
int girlsToParticipate = readTheNumberOfGirls();
int result = compute(strengthOfGroup , boysToParticipate , girlsToParticipate);
System.out.println("Number of groups that can be formed : " + result);
}
}
我已經評論了第105行。
computeFactorial
避免在R > N
調用factorial
,但是在所有其他情況下( R == N
, R < N
)調用它,並傳入NR
。 如果R == N
,則NR
為0
。 在factorial
,您要檢查num == 1
並返回1
,但是當num
為0
,將使用num - 1
(即-1
進行factorial
調用。 然后,它使用num - 1
(它是-2
再次調用自己,依此類推,並使用越來越大的負數( -1
, -2
,...),直到耗盡堆棧。
我沒有仔細閱讀代碼,但是至少當num == 0
以及num == 1
( 0! = 1
)時,您需要有factorial
返回1
。 如果您給它提供負數,我也會讓它拋出異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.