Ayuda con Recursividad
Publicado por Aitor (2 intervenciones) el 03/11/2020 22:49:55
Me dan el siguiente código y me piden que compare las matrices de las casos dados dividiendolos en vectores con Arrays.CopyOfRange y pasando los fragmentos por SonVectoresIguales, que es el método que compará los fragmentos. Todo ello usando recursividad. Si alguien sabe como puedo aplicar recursividad me haría un gran favor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class Solucion {
public static int n = 3; //la n la tomaremos como el tamaño de los vectores de la matriz
public static void main(String[] args) {
Solucion solucion = new Solucion();
solucion.problema();
}
public void problema() {
System.out.println("Inicio problema");
int[][] matriz1 = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int[][] matriz2 = {{9, 10, 11, 12},
{5, 6, 7, 8},
{1, 2, 3, 4}};
int[][] matriz3 = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
System.out.println("Test 1:");
System.out.println(String.format("Matriz1 == Matriz1: %b", sonMatricesIguales(matriz1, matriz1, matriz1.length)));
System.out.println("\n\nTest 2:");
System.out.println(String.format("Matriz1 == Matriz2: %b", sonMatricesIguales(matriz1, matriz2, matriz1.length)));
System.out.println("\n\nTest 3:");
System.out.println(String.format("Matriz2 == Matriz3: %b", sonMatricesIguales(matriz2, matriz3, matriz2.length)));
System.out.println("Fin problema\n");
}
public boolean sonMatricesIguales(int[][] matriz1, int[][] matriz2, int fila) {
boolean sonIguales = true;
} else {
return sonMatricesIguales(matriz1, matriz2, fila);
}
return sonIguales;
}
public boolean sonVectoresIguales(int[] vector1, int[] vector2) {
boolean sonIguales = true;
if (vector1[n] != vector2[n]) {
return false;
} else if (n == 0) {
return true;
} else {
n--;
// System.out.println(n);
return sonVectoresIguales(vector1, vector2);
}
}
//return sonIguales ;
public static void MatrizAVector(int m1[][], int n) {
int v1[] = {};
int i, j;
int contador = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
v1[contador] = m1[i][j];
contador = contador + 1;
System.out.println(v1);
}
}
}
}
Valora esta pregunta


0