mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package tutorialJava.examenes.examen20241025;
|
||||
|
||||
public class EjA_NumerosAmigos {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int num1 = 220;
|
||||
boolean tieneAmigo = false;
|
||||
|
||||
for(int i = num1 + 1; i < 1000000; i++) {
|
||||
if (sonNumerosAmigos(num1, i)) {
|
||||
System.out.println(num1 + " y " + i + " son amigos");
|
||||
tieneAmigo = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (tieneAmigo == false) {
|
||||
System.out.println("El número " + num1 + " no tiene amigo");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param n1
|
||||
* @param n2
|
||||
* @return
|
||||
*/
|
||||
public static boolean sonNumerosAmigos(int n1, int n2) {
|
||||
if (esAmigoDe(n1, n2) && esAmigoDe(n2, n1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param num
|
||||
* @param posibleAmigo
|
||||
* @return
|
||||
*/
|
||||
public static boolean esAmigoDe(int num, int posibleAmigo) {
|
||||
int suma = 0;
|
||||
|
||||
for (int i = 1; i < num; i++) {
|
||||
if (num % i == 0) {
|
||||
// System.out.println(i + " es divisor de " + num);
|
||||
suma += i;
|
||||
}
|
||||
}
|
||||
if (suma == posibleAmigo) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package tutorialJava.examenes.examen20241025;
|
||||
|
||||
public class EjB_ComprobarSiSumaPotencias3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int num = 30;
|
||||
int sumaPotencias3 = 0;
|
||||
int ultimoExponenteValido = 0;
|
||||
|
||||
for (int i = 1; i < 8; i++) {
|
||||
sumaPotencias3 += Math.pow(3, i);
|
||||
if (sumaPotencias3 == num) {
|
||||
ultimoExponenteValido = i;
|
||||
// System.out.println("El " + num + " es suma de potencias de 3");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ultimoExponenteValido == 0) {
|
||||
System.out.println("No se puede obtener el " + num + " como suma de potencias de 3");
|
||||
}
|
||||
else { // Sí se puede expresar como suma de potencias de 3
|
||||
for (int i = 1; i <= ultimoExponenteValido; i++) {
|
||||
System.out.print("3^" + i);
|
||||
if (i < ultimoExponenteValido) {
|
||||
System.out.print(" + ");
|
||||
}
|
||||
}
|
||||
System.out.println(" = " + num);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package tutorialJava.examenes.examen20241025;
|
||||
|
||||
public class EjC_FactorialMasCercano {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int num = 121;
|
||||
|
||||
for(int i = 0; i < 10; i++) {
|
||||
if (factorial(i) > num) {
|
||||
System.out.println("El factorial más cercano es el de " +
|
||||
(i - 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static int factorial (int n) {
|
||||
int fact = 1;
|
||||
|
||||
for (int i = 2; i <= n; i++) {
|
||||
fact *= i;
|
||||
}
|
||||
return fact;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package tutorialJava.examenes.examen20241025;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjD_OrdenacionEntreLimites {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a[] = new int[15];
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
ordenaBurbuja(a, 2, 8);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
}
|
||||
|
||||
|
||||
public static void ordenaBurbuja(int a[], int limInf, int limSup) {
|
||||
boolean hayIntercambios = false;
|
||||
do {
|
||||
hayIntercambios = false;
|
||||
for (int i = limInf; i <= limSup; 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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user