Skip to content

Instantly share code, notes, and snippets.

@LuisPalacios
Created October 30, 2022 12:27
Show Gist options
  • Save LuisPalacios/ba41d79cc69ac229ce8636076a40b43e to your computer and use it in GitHub Desktop.
Save LuisPalacios/ba41d79cc69ac229ce8636076a40b43e to your computer and use it in GitHub Desktop.
Ejercicio de detección de clusters
// LUISPA Versión con INTs
package Clusters;
public class Clusters {
private static final int LINEAS = 10;
private static final int COLUMNAS = 10;
public static void main(String[] args) {
System.out.println("LuisPa in Action !!\n");
int[][] iArrayRandom = new int[LINEAS][COLUMNAS];
int[][] iArraySalida = new int[LINEAS][COLUMNAS];
// Preparar la entrada, arrays de Randoms
for (int i = 0; i < LINEAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
iArrayRandom[i][j] = (int) (Math.random() * 10);
}
}
for (int i = 0; i < LINEAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
System.out.print(iArrayRandom[i][j] + " ");
}
System.out.println();
}
// Buscar clusters y guardarlos en iArraySalida
int iActual=-1;
int iAnterior=-1;
int numClusters=0;
boolean bCluster=false;
for (int i = 0; i < LINEAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
// Me guardo el valor de la celda actual
iActual= iArrayRandom[i][j];
// Comparo con la celda anterior
if ( iActual==iAnterior) {
iArraySalida[i][j-1] = iActual;
iArraySalida[i][j] = iActual;
if ( bCluster == false ) {
numClusters++;
}
bCluster=true;
} else {
iArraySalida[i][j] = -1;
bCluster=false;
}
// Guardo la celda actual
iAnterior = iActual;
}
}
System.out.println("===================");
for (int i = 0; i < LINEAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
if (iArraySalida[i][j]==-1 ) {
System.out.print("- ");
} else {
System.out.print(iArraySalida[i][j] + " ");
}
}
System.out.println();
}
System.out.println("===================");
System.out.println("Número de clusters: " + numClusters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment