Foros del Web » Programación para mayores de 30 ;) » Java »

Problema: no encuentro la solución

Estas en el tema de Problema: no encuentro la solución en el foro de Java en Foros del Web. mis problemas son estos: 1.Necesito el menor absoluto y el menor anterior(tengo el menor abs, pero el menor anterior no se como sacarlo). 2. necesito ...
  #1 (permalink)  
Antiguo 27/02/2012, 08:29
 
Fecha de Ingreso: febrero-2012
Mensajes: 1
Antigüedad: 12 años, 2 meses
Puntos: 0
Pregunta Problema: no encuentro la solución

mis problemas son estos:
1.Necesito el menor absoluto y el menor anterior(tengo el menor abs, pero el menor anterior no se como sacarlo).
2. necesito mostrar la ubicación del menor anterior y el absoluto en la matriz y calcular la distancia que hay entre ellos(ejemplo abs=[0][0]; menant=[0][4];
total de distancia 4 celdas)

ahy mando el codigo para que lo vean perdon si esta un poco desordenado.

package final4;
import java.util.Random;
public class Final4 {
int [][]matriz= new int [25][20];
//proceso para llenar la matriz
public void llenado() {
Random rnd = new Random();

for (int i = 0; i < 25; i++) {
for (int j = 0; j < 20; j++) {

matriz[i][j] = (int)(rnd.nextDouble() * 10.0);
}
}
}
//proceso para encontrar el menor y el menor abs
public void menor(){
int abs=99,pos,men;

for (int i = 0; i < 25; i++) {
for (int j = 0; j < 20; j++) {
men=matriz[i][j];
pos=0;
if (abs>men) {
abs=men;
}
}

}

System.out.println("el menor absoluto es: "+abs);

}
//proceso para mostrar la matriz
public void mostrar() {
for(int i=0; i<25;i++){
for(int j=0; j<20;j++){
System.out.print(matriz[i][j]+" ");
}
System.out.println();
}


}

//codigo principal
public static void main(String[] args) {
Final4 fin;
fin= new Final4();
fin.llenado();
fin.mostrar();
fin.menor();
}
}
  #2 (permalink)  
Antiguo 01/06/2012, 10:00
Avatar de Lalounam  
Fecha de Ingreso: mayo-2012
Ubicación: México D.F.
Mensajes: 59
Antigüedad: 11 años, 11 meses
Puntos: 19
Respuesta: Problema: no encuentro la solución

Mira, si te entedí bien, aquí hay un ejemplo:

Código Java:
Ver original
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.     int[][] matriz = new int[25][20];
  5.  
  6.     // proceso para llenar la matriz
  7.     public void llenado() {
  8.         Random rnd = new Random();
  9.  
  10.         for (int i = 0; i < 25; i++) {
  11.             for (int j = 0; j < 20; j++) {
  12.                 do {
  13.                     matriz[i][j] = (int) (rnd.nextDouble() * 100.0);
  14.                 } while (matriz[i][j] <= 0);
  15.             }
  16.         }
  17.     }
  18.  
  19.     // proceso para encontrar el menor y el menor abs
  20.     public void menor() {
  21.         int abs = 0, ant = 0;
  22.         int absR = 0, absC = 0;
  23.         int antR = 0, antC = 0;
  24.  
  25.         for (int i = 0; i < 25; i++) {
  26.             for (int j = 0; j < 20; j++) {
  27.                 if (abs > matriz[i][j] || abs == 0) {
  28.                     abs = matriz[i][j];
  29.                     absR = i;
  30.                     absC = j;
  31.                 } else if ((ant > matriz[i][j] && abs < matriz[i][j])
  32.                         || ant == 0) {
  33.                     ant = matriz[i][j];
  34.                     antR = i;
  35.                     antC = j;
  36.                 }
  37.             }
  38.  
  39.         }
  40.  
  41.         System.out.println("\nEl menor absoluto es: "
  42.                 + (abs < 10 ? ("0" + abs) : (abs)) + " ubicado en Renglon="
  43.                 + absR + " Columna=" + absC);
  44.  
  45.         System.out.println("\nEl menor anterior es: "
  46.                 + (ant < 10 ? ("0" + ant) : (ant)) + " ubicado en Renglon="
  47.                 + antR + " Columna=" + antC);
  48.  
  49.         System.out.println("\nDistancia = "
  50.                 + ((Math.max(antR, absR) - Math.min(antR, absR)) + ((Math.max(
  51.                         antC, absC)) - (Math.min(antC, absC)))));
  52.     }
  53.  
  54.     // proceso para mostrar la matriz
  55.     public void mostrar() {
  56.         for (int i = 0; i < 25; i++) {
  57.             for (int j = 0; j < 20; j++) {
  58.                 System.out.print((matriz[i][j] < 10 ? ("0" + matriz[i][j])
  59.                         : matriz[i][j])
  60.                         + " ");
  61.             }
  62.             System.out.println();
  63.         }
  64.  
  65.     }
  66.  
  67.     // codigo principal
  68.     public static void main(String[] args) {
  69.         Main fin;
  70.         fin = new Main();
  71.         fin.llenado();
  72.         fin.mostrar();
  73.         fin.menor();
  74.     }
  75. }

Pedí que no fueran iguales el menor absoluto y el menor anterior, por supuesto aumenté a 100 el rango de los aleatorios para hacerlo interesante y saqué la distancia en celdas como en tú ejemplo (incluyendo la celda de los números), aunque sería mejor la distancia euclidiana.

Espero te sirva, saludos!

Etiquetas: matrices
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:09.