Exercise with Recursion Search

2

My problem is this:

Certain type of pump, requires a detonator to activate. These pumps are made in such a way that only their detonator can activate them. This is done by having the detonator and the plug of this one different for each pump.

You have in your possession 20 bombs with their 20 respective detonators. However, you do not know which detonator corresponds to which pump and you need to know. You can not compare any detonator with any pump, since for this you would need to plug them in and activate the pump. You can only compare the detonators to each other, and the bombs to each other.

Take into account that in the locations of the 1 to the 20 are the sizes of the plugs of the pumps and of the 21 to the 40 , the sizes of the detonators.

I have already solved it in the following way (recursively):

public class DetonadorAndBomb {

    public static void bomb(int []a,int m,int n){
        if(a[m]==a[n+1]){
            System.out.println(a[n+1]);
            if(!(m==(n+1))){
                n=1;
                bomb(a,m+1,n);
            }

        } else{
            bomb(a,m,n+1);
        }
    }

    public static void main(String[] args) {
        int num[] = {1,3,3,1};
        bomb(num,0,1);
    }
}

And in the console it turns out:

  

1

     

3

     

3

It is assumed that you should not print the last value.

    
asked by Adrian Hernandez Islas 26.09.2016 в 01:49
source

1 answer

0

Let's see if I understood:

  

a [] = {1, 2, 3, 3, 1, 2}

Exit:

  

The position 0 ( switch 1) corresponds to the position 4 ( bomba 1)

     

The position 1 ( switch 2) corresponds to the position 5 ( bomb 2) p>      

The position 2 ( switch 3) corresponds to the position 3 ( bomba 3)

Possible recursive solution:

public class DetonadorAndBomb {

    public static void bomb(int []a,int m,int n)
    {
        int t = a.length,    // Tamaño del vector.
            h = t/2;         // Posición donde comienza la segunda parte del vector.
        // Control para que m se mantenga en la primera mitad del vector.
        if (m<h)
        {
            if(a[m]==a[n])    // Se encontró una coincidencia.
            {
                System.out.format("A la posicion %d (interruptor %d) le corresponde "+
                                  "la posicion %d (bomba %d)\n", m, a[m], n, a[n]);
                bomb(a,m+1,h);    /* La posición m ya tuvo coincidencia con
                                     la posición n, entonces se sigue buscando,
                                     para las siguientes posiciones, comenzando en m+1 */
            }
            else if (n < t-1)     /* No hubo coincidencia, entonces se controla que
                                     haya posiciones disponibles en la segunda parte
                                     del vector para seguir buscando posibles
                                     coincidencias para la posición m */
                bomb(a,m,n+1);
            else                  /* No hubo coincidencia y no quedan posiciones
                                     disponibles en la segunda mitad del vector,
                                     por lo que la búsqueda debe continuar desde m+1. */
                bomb(a,m+1,h);
        }
    }

    public static void main(String[] args) {
        int num[] = {1,2,3,3,1,2};

        bomb(num,0,num.length/2);
    }
}

Code available with debugging messages to visually see how the recursive function works.

    
answered by 05.10.2016 / 23:42
source