How to recursive the authomata Strange Planet exercise?

放肆的年华 提交于 2019-12-13 12:45:38

问题


The basic idea of this is that in a planet there is three diferent kinds of species, only two of the three species can came together to procreate, and the result is that this to species die a two new sobjects of the third species born, for example we have a b and c, and the species a and b came together and let 2 c new members born.

It is like: 1a 1b and 1c (Sorry for the lenguage)

When a and b want to have kids they came together and die but have two kids, these new kids are from the species c, so the result is:

0a 0b and 3 c

In this casie we said that the planet succed and c is the dominating spcecies. But when I have 3a 4b 2c and I need to see if any of the three species could succed in the planet.

I thought that I could do it with a recursive solution, but I get always the error of:

Exception in thread "main" java.lang.StackOverflowError

In the function I am trying to use recursive.

This is my code, I know something is wrong but I don´t know what.

public class Automata2 {

public static void main(String[] args) {
    int N = 2;
    while (N <= 14) {            
        partition(N);
        N++;
    }

}

public static void partition(int N) {
    int n1,n2,n3;
    for(n1=0;n1<=N;n1++){
        for(n2=0;n2<=N;n2++){
            for(n3=0;n3<=N;n3++){
                if((n1+n2+n3)==N){

                    strPlanetA(n1, n2, n3);

                }
            }
        }
    }
}

public static void strPlanetA(int a, int b, int c){
    int a2  = a, b2 = b, c2 = c;


    while (!(a2!=0 && b2==0 && c2==0)) {            

        a2=a2+2;
        b2--;
        c2--;

        if (a2==a && b2==b && c2==c) {
            System.out.println("Not Fail");
        }
        if (a2!=0 && b2==0 && c2==0) {
            System.out.println("Fail in A");
        }

        strPlanetA(a2, b2, c2);
    }       
}

}

The partition is only to get all the population posible in the planet, I need to see if the plane succed in a planet with population from 2 until 14.


回答1:


A stack overflow error is a usual synonym for infinite recursion/loop. Basically your function here strPlanetA calls itself at the end of the code, regardless of what happened before... and again... and again... and so on endlessly. You need a condition that when verified, prevent the recursive call.

Your code is basically flawed in its reasoning. My very strong suggestion if you want to code recursive functions, is to do the math first. You need acquire a minimal intuition in iterative reasoning.

For example you have a double nested recursion, one with the recursive call, one with the loop. I don't see why on earth you would need this. You can just drop the recursive call altogether.




回答2:


Basically what I wanted to achive was this, there shoul be better ways on how to improve this.

public class Automata2 {

/**
 * @param args the command line arguments
 */

static int cont = 0;

public static void main(String[] args) {
    int N = 2;
    while (N <= 14) {            
        partition(N);
        N++;
    }
    System.out.println("Cantidad de Respuestas= " + cont);
}

public static void partition(int N) {
    int n1,n2,n3;
    for(n1=0;n1<=N;n1++){
        for(n2=0;n2<=N;n2++){
            for(n3=0;n3<=N;n3++){
                if((n1+n2+n3)==N){

                    if (strPlanetA(n1, n2, n3) == 1) {
                        cont++;
                        System.out.print("A falla en: ");
                        System.out.print(n1+"-");
                        System.out.print(n2+"-");
                        System.out.println(n3);
                    }

                    if (strPlanetB(n1, n2, n3) == 1) {
                        cont++;
                        System.out.print("B falla en: ");
                        System.out.print(n1+"-");
                        System.out.print(n2+"-");
                        System.out.println(n3);
                    }

                    if (strPlanetC(n1, n2, n3) == 1) {
                        cont++;
                        System.out.print("C falla en: ");
                        System.out.print(n1+"-");
                        System.out.print(n2+"-");
                        System.out.println(n3);
                    }
                }
            }
        }
    }
}

public static int strPlanetA(int a2, int b2, int c2){
    if((a2!=0 && b2==0 && c2==0)||(c2 == b2)){
        return 1;
    }

    if (b2>0 && c2>0) {
        a2=a2+2;
        b2--;
        c2--;
        if (a2!=0 && b2==0 && c2==0) {
            return 1;
        }
        else{
            strPlanetA(a2, b2, c2);
        }
    }
    return 3;
}

public static int strPlanetB(int a2, int b2, int c2){
    if((a2==0 && b2!=0 && c2==0)||(c2 == a2)){
        return 1;
    }

    if (a2>0 && c2>0) {
        a2--;
        b2=b2+2;
        c2--;
        if (a2==0 && b2!=0 && c2==0) {
            return 1;
        }
        else{
            strPlanetB(a2, b2, c2);
        }
    }
    return 3;
}

public static int strPlanetC(int a2, int b2, int c2){
    if((a2==0 && b2==0 && c2!=0)||(a2 == b2)){
        return 1;
    }

    if (a2>0 && b2>0){
        a2--;
        b2--;
        c2=c2+2;
        if (a2==0 && b2==0 && c2!=0) {
            return 1;
        }
        else{
            return strPlanetC(a2, b2, c2);
        }
    }
    return 3;
}  

}

Thanks for the recomendation aobve the recursive, it was totally a mess.



来源:https://stackoverflow.com/questions/25109050/how-to-recursive-the-authomata-strange-planet-exercise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!