mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 09:57:40 +01:00
feat(examen 19/6/25): solucionado
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class Ej02_OrdenarArrayPorDivisores {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int a[] = inicializaArray();
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
|
||||||
|
ordenaArrayPorDivisores(a);
|
||||||
|
|
||||||
|
UtilsArrays.imprimeArray(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static int[] inicializaArray () {
|
||||||
|
int a[] = new int[100];
|
||||||
|
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
a[i] = Utils.obtenerNumeroAzar(0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
// return new int[] {11, 22, 20, 12, 8};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param n
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static int countDivisores(int n) {
|
||||||
|
int contadorDivisores = 0;
|
||||||
|
for (int i = 2; i < n; i++) {
|
||||||
|
if (n % i == 0) {
|
||||||
|
contadorDivisores++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return contadorDivisores;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param a
|
||||||
|
*/
|
||||||
|
private static void ordenaArrayPorDivisores (int a[]) {
|
||||||
|
boolean hayIntercambios;
|
||||||
|
do {
|
||||||
|
hayIntercambios = false;
|
||||||
|
for (int i = 0; i < (a.length - 1); i++) {
|
||||||
|
if (countDivisores(a[i]) > countDivisores(a[i + 1])) {
|
||||||
|
int aux = a[i];
|
||||||
|
a[i] = a[i + 1];
|
||||||
|
a[i + 1] = aux;
|
||||||
|
hayIntercambios = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (hayIntercambios == true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz;
|
||||||
|
|
||||||
|
public class Ej03_StringDeCamelCaseASnakeCase {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "estoEsUnEjemplo";
|
||||||
|
System.out.println("Cadena modificada: " + camelCaseToSnakeCase(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String camelCaseToSnakeCase (String str) {
|
||||||
|
// StringBuffer sb = new StringBuffer();
|
||||||
|
String sb = "";
|
||||||
|
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
// Compruebo si es una mayúscula
|
||||||
|
if (str.charAt(i) >= 65 && str.charAt(i) <= 90) {
|
||||||
|
// sb.append("_");
|
||||||
|
sb += "_";
|
||||||
|
char minuscula = (char) (str.charAt(i) + 32);
|
||||||
|
// sb.append(minuscula);
|
||||||
|
sb += minuscula;
|
||||||
|
}
|
||||||
|
else { // No es una mayúscula
|
||||||
|
// sb.append(str.charAt(i));
|
||||||
|
sb += str.charAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
import tutorialJava.UtilsArrays;
|
||||||
|
|
||||||
|
public class ElementosMasFrecuentesEnMatriz {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int m[][] = inicializaMatriz();
|
||||||
|
UtilsArrays.imprimeMatriz(m);
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
HashMap<Integer, Integer> hm = creaHashMapDeRepeticiones(m);
|
||||||
|
|
||||||
|
int mRepeticiones[][] = getMatrizRepeticionesDesdeHashMap(hm);
|
||||||
|
UtilsArrays.imprimeMatriz(mRepeticiones);
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
ordenarMatrizRepeticiones(mRepeticiones);
|
||||||
|
UtilsArrays.imprimeMatriz(mRepeticiones);
|
||||||
|
|
||||||
|
// Sacamos en consola los x que más se repiten
|
||||||
|
int podium = 3;
|
||||||
|
for (int i = 0; i < podium; i++) {
|
||||||
|
System.out.println("Número más repetido: " +
|
||||||
|
mRepeticiones[i][0] + " con " +
|
||||||
|
mRepeticiones[i][1] + " repeticiones");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static int[][] inicializaMatriz () {
|
||||||
|
int filas = Utils.obtenerEnteroConDescripcion(
|
||||||
|
"Introduzca filas de la matriz: ");
|
||||||
|
int columnas = Utils.obtenerEnteroConDescripcion(
|
||||||
|
"Introduzca columnas de la matriz: ");
|
||||||
|
|
||||||
|
int m[][] = new int[filas][columnas];
|
||||||
|
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
m[i][j] = Utils.obtenerNumeroAzar(0, 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// int m[][] = new int[][] {{2, 3, 4, 4},
|
||||||
|
// {2, 3, 5, 5},
|
||||||
|
// {5, 4, 6, 7},
|
||||||
|
// {6, 7, 4, 5}};
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static HashMap<Integer, Integer>
|
||||||
|
creaHashMapDeRepeticiones(int m[][]) {
|
||||||
|
|
||||||
|
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
|
||||||
|
|
||||||
|
for (int i = 0; i < m.length; i++) {
|
||||||
|
for (int j = 0; j < m[i].length; j++) {
|
||||||
|
int valorAComprobar = m[i][j];
|
||||||
|
|
||||||
|
// Compruebo si el valorAComprobar ya existe, o no, como
|
||||||
|
// key del hashmap
|
||||||
|
if (hm.get(valorAComprobar) == null) {
|
||||||
|
// En el HashMap no hay key que tenga el "valorAComprobar"
|
||||||
|
hm.put(valorAComprobar, 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Ya existe un key de "valorAComprobar" en el hm
|
||||||
|
int repeticionesDeValorAComprobar = hm.get(valorAComprobar);
|
||||||
|
hm.put(valorAComprobar, repeticionesDeValorAComprobar + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param hm
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static int[][] getMatrizRepeticionesDesdeHashMap(
|
||||||
|
HashMap<Integer, Integer> hm) {
|
||||||
|
|
||||||
|
int mRepeticiones[][] = new int[hm.values().size()][2];
|
||||||
|
|
||||||
|
Iterator<Integer> keys = hm.keySet().iterator();
|
||||||
|
int i = 0;
|
||||||
|
while (keys.hasNext()) {
|
||||||
|
int singleKey = keys.next();
|
||||||
|
mRepeticiones[i][0] = singleKey;
|
||||||
|
mRepeticiones[i][1] = hm.get(singleKey);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return mRepeticiones;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
*/
|
||||||
|
private static void ordenarMatrizRepeticiones (int m[][]) {
|
||||||
|
boolean hayIntercambios;
|
||||||
|
|
||||||
|
do {
|
||||||
|
hayIntercambios = false;
|
||||||
|
for (int i = 0; i < (m.length - 1); i++) {
|
||||||
|
if (m[i][1] < m[i + 1][1]) {
|
||||||
|
int aux[] = m[i];
|
||||||
|
m[i] = m[i + 1];
|
||||||
|
m[i + 1] = aux;
|
||||||
|
hayIntercambios = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (hayIntercambios == true);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user