1

_以下の質問の次のプログラムでは、一連の例外がException in thread "main" java.lang.StackOverflowError at testing_package.Compute.factorial(Compute.java:105)発生します。なぜこのエラーが発生するのかわかりません。

質問: N 人の男の子と M 人の女の子が演劇で演技のスキルを学んでいます。劇を上演するには、4 人以上の男の子と 1 人以上の女の子を含む P アクターのグループを形成する必要があります。劇場では、グループを形成できる方法の数を伝えるプログラムを作成する必要があります。注: 構成は一意である必要があり、構成が構成されている順序ではありません。

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行目にコメントしました。 ここに画像の説明を入力

4

1 に答える 1

6

computeFactorialfactorialifの呼び出しを回避R > Nしますが、他のすべてのケース ( R == N, R < N) で呼び出し、 を渡しN-Rます。の場合R == NN-Rです0。ではfactorial、 if をチェックしてnum == 1を返します1が、numis0の場合、 is で自分自身をfactorial呼び出してnum - 1います-1。次に、スタックがなくなるまで、ますます大きくなる負の数 ( 、 、 ...) を使用してnum - 1、 で再び自分自身を呼び出します。-2-1-2

私はコードを詳しく読んでいませんが、少なくともwhenと when ( ) をfactorial返す必要があります。また、負の数を入力すると例外がスローされます。1num == 0num == 10! = 1

于 2012-02-23T06:40:30.063 に答える