通过数组递归函数直到结束

时间:2014-01-16 23:41:45

标签: java

我有一个数组,如

int[] array = {4,1,1,1,1,4};

前4个表示有4个数字需要计算,后4个表示我们需要找到一个操作组合来实现该数字。

我希望在到达最后1时使循环停止,或者通常是数组中最后一个之前的数字。通常这很容易,但是当所有数字都相同时,最后找到其中一个的索引很困难......建议?

我通常使用:

int last_num = integers[integers.length-2];


if(last_num == integers[a]){ System.out.println(Total); )

但是如果last_num = 1,并且整数[a] = 1,那么它可以在第一次进入循环后停止。

如果因为所有数字相同而试图使用binarySearch找到索引,则会发生同样的事情。

递归函数(注意我是Java新手,我确定我使用的函数是错误的,因为我每次只返回一个真值,但我的目标是让它运行并打印出我想要的值 - 请原谅现在):

public static boolean addMoreMore(int a , double b , String c, int[] integers , int target, double possible2, int last_num){
  int counter = 0;
  char[] symbols1 = {'+' , '-' , '*','/'};            
  for(int z = 0; z<4; z++){
        char operator = symbols1[z];

        String what1 = ""; 
        double total1 = 0;            

        if(operator == '+'){
            total1 = b + integers[a];
            what1 = c + "+" + integers[a];  //System.out.println(what1);    
            if(last_num  != integers[a]){
                //System.out.println("ACTIVATE1");
                addMoreMore(a+1 , total1, what1, integers, target, possible2, last_num);
         }
        else {
            if(total1 == target){
                System.out.println(what1 + " = " + total1);     
            }
        }
  }    
        else if(operator == '-'){

            total1 = b - integers[a];
            what1 = c + "-" + Integer.toString(integers[a]);       
            //System.out.println(what1);                                   

            if(last_num  != integers[a]){
                //     System.out.println("ACTIVATE2");
                addMoreMore(a+1 , total1, what1, integers, target, possible2, last_num);  
            }
            else {
                if(total1 == target){
                    System.out.println(what1 + " = " + total1);
                }          
            }
      }
      else if(operator == '*'){
          total1 = b * integers[a];
          what1 = c + "*" + Integer.toString(integers[a]);  
          //System.out.println(what1);                                

          if(last_num  != integers[a]){
              //       System.out.println("ACTIVATE3");
              addMoreMore(a+1 , total1, what1, integers, target, possible2, last_num);       
          }
          else{
              if(total1 == target){
                  System.out.println(what1 + " = " + total1);
              }   
          }
     }
     else if(operator == '/'){
         total1 = b / integers[a];
         what1 = c + "/" + Integer.toString(integers[a]); // System.out.println(what1);  
         if((b % integers[a]) == 0){

             if(last_num  != integers[a]){
                 // System.out.println("ACTIVATE4");
                addMoreMore(a+1 , total1, what1, integers, target, possible2, last_num);  
             }
          else {
              if(total1 == target){
                  System.out.println(what1 + " = " + total1);    
              }
          }
      }    
  }         
}
return true;       
}

2 个答案:

答案 0 :(得分:1)

如评论所述,比较数组的索引位置而不是值。

例如:

而不是

if(last_num == integers[a]){ System.out.println(Total); )

尝试

if(integers.length - 2 == a) { System.out.println(Total); }

假设a是数组中索引位置的计数变量。

答案 1 :(得分:0)

import java.util.ArrayList;
import java.util.LinkedList;

public class Q21175220 {
    static enum Operation {
        ADD,
        SUBTRACT,
        MULTIPLY,
        DIVIDE
    }

    public static void getValidOperations
    (
        final ArrayList<Operation[]> validOperations,
        final LinkedList<Operation> operations,
        final int[] values,
        final int desired_total
    )
    {
        if ( operations.size() < values.length - 1 )
        {
            operations.addLast( Operation.ADD );
            getValidOperations( validOperations, operations, values, desired_total );
            operations.removeLast();
            operations.addLast( Operation.SUBTRACT );
            getValidOperations( validOperations, operations, values, desired_total );
            operations.removeLast();
            operations.addLast( Operation.MULTIPLY );
            getValidOperations( validOperations, operations, values, desired_total );
            operations.removeLast();
            operations.addLast( Operation.DIVIDE );
            getValidOperations( validOperations, operations, values, desired_total );
            operations.removeLast();
        } else {
            int i = 0;
            int total = values[i];
            for ( Operation op : operations )
            {
                ++i;
                switch ( op )
                {
                case ADD:       total += values[i]; break;
                case SUBTRACT:  total -= values[i]; break;
                case MULTIPLY:  total *= values[i]; break;
                case DIVIDE:    total /= values[i]; break;
                }
            }
            if ( total == desired_total )
                validOperations.add( operations.toArray( new Operation[ values.length - 1 ] ) );
        }
    }

    public static String calculationToString
    (
        final Operation[] operations,
        final int[] values
    )
    {
        final StringBuffer buffer = new StringBuffer();
        int i = 0;
        buffer.append( values[i] );
        for ( Operation op : operations )
        {
            switch ( op )
            {
            case ADD:       buffer.append('+'); break;
            case SUBTRACT:  buffer.append('-'); break;
            case MULTIPLY:  buffer.append('*'); break;
            case DIVIDE:    buffer.append('/'); break;
            }
            buffer.append( values[++i] );
        }
        return buffer.toString();
    }

    public static void test
    (
        final int[] values,
        final int desired_total
    )
    {
        final ArrayList<Operation[]> validOperations = new ArrayList<Operation[]>();
        getValidOperations( validOperations, new LinkedList<Operation>(), values, desired_total );
        for ( Operation[] ops: validOperations )
            System.out.println( calculationToString( ops, values ) + " = " + desired_total );
    }
    public static void main(String[] args) {
        test( new int[]{ 1, 1, 1, 1 }, 4 );
        test( new int[]{ 1, 1, 1, 2, 5 }, 10 );
    }
}