mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-10 02:13:07 +01:00
feat(examen 20241025): solved
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Ej03_EjemploMatrices {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// primeraFormaDeclararUnaMatri();
|
||||
segundaFormaDeclararUnaMatri();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void primeraFormaDeclararUnaMatri() {
|
||||
int m[][] = new int[4][3];
|
||||
// int m2[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
|
||||
|
||||
int cont = 1;
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
// m[i][j] = (int) Math.round(Math.random() * 100);
|
||||
m[i][j] = cont;
|
||||
cont++;
|
||||
m[i][j] = (int) Math.round(Math.random() * 100);
|
||||
}
|
||||
}
|
||||
|
||||
m[0][2] = 1;
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
@@ -27,4 +32,30 @@ public class Ej03_EjemploMatrices {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void segundaFormaDeclararUnaMatri() {
|
||||
int m[][] = new int[][] {{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
{10, 11, 12}};
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ public class Ej04_ComoHacerEjerciciosMatrices {
|
||||
{10, 0, 12, 13, 14},
|
||||
{0, 16, 17, 18, 19},
|
||||
{20, 21, 22, 0, 24} };
|
||||
|
||||
|
||||
// int m[][] = new int[5][5];
|
||||
// inicializaMatrizAlAzar(m);
|
||||
imprimeMatriz(m);
|
||||
System.out.println("Es positiva: " + esMatrizPositiva(m));
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque03;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjE_PicosDeUnArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a[] = new int[] {7, 3, 20, 4, 6, 0, 5};
|
||||
|
||||
int picos[] = picosDeArray(a);
|
||||
UtilsArrays.imprimeArray(picos);
|
||||
}
|
||||
|
||||
|
||||
public static int[] picosDeArray(int a[]) {
|
||||
int contPicos = 0;
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (i == 0 && a[0] > a[1]) {
|
||||
contPicos++;
|
||||
}
|
||||
else {
|
||||
if (i == (a.length - 1) && a[a.length-1] > a[a.length-2]) {
|
||||
contPicos++;
|
||||
}
|
||||
else {
|
||||
if (a[i] > a[i-1] && a[i] > a[i+1]) {
|
||||
contPicos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int picos[] = new int[contPicos];
|
||||
int indiceArrayPicos = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (i == 0 && a[0] > a[1]) {
|
||||
picos[indiceArrayPicos] = a[i];
|
||||
indiceArrayPicos++;
|
||||
}
|
||||
else {
|
||||
if (i == (a.length - 1) && a[a.length-1] > a[a.length-2]) {
|
||||
picos[indiceArrayPicos] = a[i];
|
||||
indiceArrayPicos++;
|
||||
}
|
||||
else {
|
||||
if (a[i] > a[i-1] && a[i] > a[i+1]) {
|
||||
picos[indiceArrayPicos] = a[i];
|
||||
indiceArrayPicos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return picos;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque03;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ejemplo04_OrdenacionSeleccion {
|
||||
|
||||
public static void main (String args[]) {
|
||||
// int a[] = new int[] {33, 17, 72, 11, 35};
|
||||
int a[] = new int[100];
|
||||
int posicionMenor, aux;
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
// Asumo que el menor es la posición de i
|
||||
posicionMenor = i;
|
||||
|
||||
// Busco la posición REAL del menor, a partir de la posición i
|
||||
for (int j = i + 1; j < a.length; j++) {
|
||||
if (a[posicionMenor] > a[j]) {
|
||||
posicionMenor = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Intercambio
|
||||
aux = a[i];
|
||||
a[i] = a[posicionMenor];
|
||||
a[posicionMenor] = aux;
|
||||
}
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque03;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ejercicio01_OrdenacionBurbuja {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// int a[] = new int[] {33, 17, 72, 11, 35};
|
||||
int a[] = new int[1000];
|
||||
boolean hayIntercambios = false;
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
do {
|
||||
hayIntercambios = false;
|
||||
for (int i = 0; i < (a.length - 1); i++) {
|
||||
if (a[i] > a[i + 1]) {
|
||||
int aux = a[i];
|
||||
a[i] = a[i + 1];
|
||||
a[i + 1] = aux;
|
||||
hayIntercambios = true;
|
||||
}
|
||||
}
|
||||
} while (hayIntercambios == true);
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque03;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ejercicio02_OrdenacionInsercionDirecta {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// int a[] = new int[] {33, 17, 72, 11, 35};
|
||||
int a[] = new int[1000];
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
for (int i = 1; i < a.length; i++) {
|
||||
int key = a[i];
|
||||
int j = i - 1;
|
||||
|
||||
// Mueve los elementos del array que son mayores que la clave
|
||||
// a una posición adelante de su posición actual
|
||||
while (j >= 0 && a[j] > key) {
|
||||
a[j + 1] = a[j];
|
||||
j = j - 1;
|
||||
}
|
||||
a[j + 1] = key;
|
||||
}
|
||||
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque03;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ejercicio03_OrdenacionBurbujaShell {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// int a[] = new int[] {33, 17, 72, 11, 35};
|
||||
int a[] = new int[1000];
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
// Define los intervalos iniciales
|
||||
for (int intervalo = a.length / 2; intervalo > 0; intervalo /= 2) {
|
||||
for (int i = intervalo; i < a.length; i++) {
|
||||
int aux = a[i];
|
||||
int j;
|
||||
for (j = i; j >= intervalo && a[j - intervalo] > aux; j -= intervalo) {
|
||||
a[j] = a[j - intervalo];
|
||||
}
|
||||
a[j] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user