【问题标题】:How to recursive the authomata Strange Planet exercise?如何递归 authomata Strange Planet 练习?
【发布时间】:2014-08-03 21:00:07
【问题描述】:

其基本思想是,在一个星球上存在三种不同的物种,这三种物种中只有两种可以聚集在一起生育,结果就是这对物种来说是第三物种的两个新对象。出生,例如我们有a b和c,物种a和b聚集在一起,让2 c新成员出生。

就像:1a 1b 和 1c (对不起语言)

当 a 和 b 想要生孩子时,他们走到一起死了,但有了两个孩子,这些新孩子来自 c 物种,所以结果是:

0a 0b 和 3c

在这个案例中,我们说行星成功并且 c 是主导物种。但是当我有 3a 4b 2c 时,我需要看看这三个物种中的任何一个能否在这个星球上取得成功。

我认为我可以使用递归解决方案来做到这一点,但我总是得到以下错误:

Exception in thread "main" java.lang.StackOverflowError

在我尝试使用递归的函数中。

这是我的代码,我知道有问题,但我不知道是什么。

公共类 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);
    }       
}

}

分区只是为了得到这个星球上所有可能的人口,我需要看看飞机是否在一个人口从2到14的星球上成功。

【问题讨论】:

    标签: java recursion automata


    【解决方案1】:

    堆栈溢出错误通常是无限递归/循环的同义词。基本上你的函数strPlanetA在代码末尾调用自己,不管之前发生了什么......然后再次......再次......等等无休止。您需要一个条件,在验证后阻止递归调用。

    您的代码在推理方面基本上存在缺陷。如果你想编写递归函数,我非常强烈的建议是先做数学。您需要在迭代推理中获得最低限度的直觉。

    例如,您有一个双嵌套递归,一个带有递归调用,一个带有循环。我不明白你到底为什么需要这个。您可以完全放弃递归调用。

    【讨论】:

    • 我试过了,如果我的三个变量不在我需要的地方,我会再次调用,否则我不会
    【解决方案2】:

    基本上我想要实现的是这个,应该有更好的方法来改进它。

    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;
    }  
    

    }

    感谢递归的推荐,这完全是一团糟。

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2017-03-09
      • 2012-03-22
      相关资源
      最近更新 更多