检查数组中是否存在使用递归满足预定条件的元素序列

时间:2014-06-06 21:38:00

标签: java arrays recursion

条件在int数组中给出,其中每个数字代表一个条件。

条件: 0 - 一位数字或2位数字。 1 - 一位数字。 2 - 2位数字。

功能减速:

布尔匹配(Integer [] inputArray,Integer [] pattern)

如果条件数组是{1,0,2},我需要检查是否存在一位数字的序列,一行或两位数字和一行中的两位数字。

我没有找到一种方法来阻止递归,当它成功找到模式的序列时它一直持续到我得到一个不受欢迎的错误。

没有设法做到这一点或想办法让它完成然后得到真实(最后总是得到假)

非常感谢你的帮助!

public static boolean match(Integer[] a,Integer[] pattern)
    {
        int _counter = 0;
        int _variNum = 0;
        Integer[] _copyA= new Integer[a.length];_copyA=a;
        Integer[] _copyPattern= new Integer[pattern.length];_copyPattern=pattern;
        return match(_copyA,_copyPattern,pattern.length,a,pattern);
    }


    public static boolean match(Integer[] secA,Integer[] secPattern,int originPatternLength,Integer[] originA,Integer[] originPattern)
    {
        boolean success=false;
        System.out.println("\n");
        System.out.println("First array: "+Arrays.toString(secPattern)+" Second array: "+Arrays.toString(secA)+" to the rules...");
        System.out.println("_variNum: *"+_variNum+"* secPattern.length: *"+secPattern.length+"*     counter: "+_counter);

        success=(((originA.length-_counter)+_variNum)<originPatternLength)?false:true;
        success=(originPatternLength ==_variNum)?true:false;

        _counter++;
        if(secPattern.length>0)
        {
            switch ((secPattern[0]))
                {
                    case 0:
                        if(!(secA[0]>-100 && secA[0]<100))
                        {
                            Integer[] newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0;
                            match(newArr,secPattern,originPatternLength,originA,originPattern);
                        }
                        _variNum++;
                        Integer[] newArr = Arrays.copyOfRange(secA, 1, secA.length);
                        Integer[] newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length);
                        match(newArr,newPArr,originPatternLength,originA,originPattern);

                    break;
                    case 1:
                        if(!(secA[0]>-10 && secA[0]<10))
                        {
                            newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0;
                            match(newArr,secPattern,originPatternLength,originA,originPattern);
                        }
                        _variNum++;
                        newArr = Arrays.copyOfRange(secA, 1, secA.length);
                        newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length);
                        return match(newArr,newPArr,originPatternLength,originA,originPattern);
                    break;
                    case 2:
                        if(!(secA[0]>-100 && secA[0]<100&&(secA[0]>10||secA[0]<-10)))
                        {
                            newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0;
                            match(newArr,secPattern,originPatternLength,originA,originPattern);
                        }
                        _variNum++;
                        newArr = Arrays.copyOfRange(secA, 1, secA.length);
                        newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length);
                        match(newArr,newPArr,originPatternLength,originA,originPattern);
                    break;
                    default:
            }
        }

        return success;
    }`

1 个答案:

答案 0 :(得分:0)

尝试使用f.f.g代码:

     public AnyClass(){
        private int a[];
        private int b[];
        String y ;


        public AnyClass(int c){
        a = new int[]{1,0,2};
        b = new int[c];
        }

        public void input(){
        for(int i = 0; i < c; i++){
        b[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number"));
        }



        public String patternLocator(){
        for(int i = 0; i < b.length; i++){
        int c2 = 0;
        y = "";
        for(int c = i; c < i+3; c++){

        if(a[c2] == 1){
        if(b[c] < 10){
        y = y + "yes";
        }

    if(a[c2] == 2){
    if(b[c] > 9 && b[c] < 100){
      y = y + "yes";
    }
    }


    if(a[c2] == 0){
    if(b[c] < 100){
    y = y + "yes";
    }
    }
    }
    if(y.equals("yesyesyes")){
    y = true;
    return y;
    break;
    }
    c2++;
    }
     y = "false";
     return y;
    }

public static void main(String args[]){
int length = Integer.parseInt("Enter length of pattern you are about to enter");
AnyClass ob = new AnyClass(lenght);
ob.input;
String ans = ob.patternLocator();
if(ans = "true"){
System.out.println("pattern found");
}
else{
System.out.println("Pattern not found");
}
}

        }