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

Consulta, array multidimensional

Estas en el tema de Consulta, array multidimensional en el foro de Java en Foros del Web. Hola a todos !!! me podrían dar una manito con esto por favor, me vuelvo loca!! estoy haciendo un ejercicio que me pide: Desarrollar un ...
  #1 (permalink)  
Antiguo 14/06/2014, 21:23
 
Fecha de Ingreso: junio-2014
Mensajes: 1
Antigüedad: 9 años, 10 meses
Puntos: 0
Consulta, array multidimensional

Hola a todos !!! me podrían dar una manito con esto por favor, me vuelvo loca!!
estoy haciendo un ejercicio que me pide:

Desarrollar un programa Java que permita ingresar los votos obtenidos por cada uno de los N candidatos a Gobernador de una provincia, en cada uno de los M distritos de la misma. El programa deberá:
a) Imprimir una tabla (matriz de orden N x M ) distritos vs. candidatos con los votos emitidos.
b) Calcular e imprimir el número total de votos recibidos por cada candidato y el porcentaje del total de votos emitidos. Asimismo, deberá visualizar el candidato más votado.
c) Si algún candidato recibe más del 50% de los votos, el programa imprimirá un mensaje declarándole ganador.
d) Si ningún candidato recibe más del 50% de los votos, el programa debe imprimir los dos candidatos más votados.

La a si me cumple, en la b me muestra al menos votado en lugar del mas votado ._., en la c me manda al perdedor como ganador ._., y la d me manda al menos votado tambien ._.

Código:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Elecciones {

private int N;
private int M;
private int CANDIDATOS_BALOTAJE = 2;
private double PORC_MIN_GANADOR = 0.5;
private String[] CANDIDATOS;
private String[] DISTRITOS;
private int[][] ESCRUTINIO;
private int[] VOTOS_X_CANDIDATO;
private BufferedReader in;
public static byte IZQUIERDA = (byte) 0;
public static byte DERECHA = (byte) 1;

public Elecciones() throws IOException {
N = leerInt("Introduzca la cantidad de candidatos: ");
M = leerInt("Introduzca la cantidad de distritos: ");
CANDIDATOS = new String[N];
DISTRITOS = new String[M];
ESCRUTINIO = new int[DISTRITOS.length][CANDIDATOS.length];
VOTOS_X_CANDIDATO = new int[CANDIDATOS.length];
for (int i = 0; i < N; i++) {
CANDIDATOS[i] = "Candidato" + (i + 1);
}
for (int i = 0; i < M; i++) {
DISTRITOS[i] = "Distrito" + (i + 1);
}
}

private String leer(String m) throws IOException {
if (in == null) {
in = new BufferedReader(new InputStreamReader(System.in));
}
if (m != null) {
System.out.print(e);
}
return in.readLine();
}

private int leerInt(String m) throws IOException {
return Integer.parseInt(leer(e));
}

void cargarDatos() throws IOException {
System.out.println("\nIngrese la cantidad de votos obtenidos");
for (int j = 0; j < DISTRITOS.length; j++) {
String distrito = DISTRITOS[j];
String linea = repetir('-', distrito.length());
System.out.println(linea);
System.out.println(distrito);
System.out.println(linea);
for (int i = 0; i < CANDIDATOS.length; i++) {
ESCRUTINIO[j][i] = leerInt(CANDIDATOS[i] + ": ");
}
}
}

public void mostrar() throws IOException {
leer("\nDatos ingresados correctamente. **Presione enter para continuar.");
System.out.println(repetir('-', (CANDIDATOS.length + 1) * 12));
System.out.println(" " + centrar("Candidatos", CANDIDATOS.length * 12));
System.out.println(repetir('-', (CANDIDATOS.length + 1) * 12));
System.out.print(alinear("Distritos", 12, IZQUIERDA));
for (int i = 0; i < N; i++) {
System.out.print(alinear(CANDIDATOS[i], 12, DERECHA));
}
System.out.println("\n" + repetir('-', (CANDIDATOS.length + 1) * 12));
for (int j = 0; j < DISTRITOS.length; j++) {
System.out.print(alinear(DISTRITOS[j], 12, IZQUIERDA));
for (int i = 0; i < CANDIDATOS.length; i++) {
System.out.print(alinear("" + ESCRUTINIO[j][i], 12, DERECHA));
}
System.out.println();
}
System.out.println(repetir('-', (CANDIDATOS.length + 1) * 12));
}

public void votosPorCandidato() {
double total = 0;
for (int j = 0; j < ESCRUTINIO.length; j++) {
for (int i = 0; i < CANDIDATOS.length; i++) {
int v = ESCRUTINIO[j][i];
VOTOS_X_CANDIDATO[i] += v;
total += v;
}
}
int mas_votado = -1;
int ganador = -1;
System.out.println("\nTotal de votos recibidos para cada candidato y su porcentaje");
System.out.println(repetir(' ', 12) + centrar("Votos", 12) + centrar("Porcentaje", 12));
int[] clasificacion = clasificarCandidatosPorVoto();
for (int i = clasificacion.length - 1; i >= 0; i--) {
int j = clasificacion[i];
int votos = VOTOS_X_CANDIDATO[j];
double porc = votos / total;
if (mas_votado < 0 || votos < VOTOS_X_CANDIDATO[mas_votado]) {
mas_votado = i;
}
if (porc > PORC_MIN_GANADOR) {
ganador = i;
}
System.out.print(alinear(CANDIDATOS[j], 12, IZQUIERDA));
System.out.print(alinear(votos, 12, DERECHA));
System.out.println(alinear(String.format("%3.2f%%" , porc * 100), 12, DERECHA));
}
if (mas_votado > 0) {
System.out.println("\nEl candidato mas votado: ");
System.out.println("\t" + CANDIDATOS[mas_votado] + ": " + VOTOS_X_CANDIDATO[mas_votado]);
}
if (ganador > 0) {
System.out.println("\nResulto ganador el:");
System.out.println("\t" + CANDIDATOS[ganador] + ": " + VOTOS_X_CANDIDATO[ganador]);
} else {
System.out.println("\nLos dos mas votados fueron:");
for (int i = clasificacion.length - 1; i >= 0 && i + CANDIDATOS_BALOTAJE >= clasificacion.length; i--) {
System.out.println("\t" + CANDIDATOS[clasificacion[i]]);
}
}
}

private void copiarArray(int[] orig, int[] dest) {
for (int i = 0; i < orig.length; i++) {
orig[i] = dest[i];
}
}

private int[] clasificarCandidatosPorVoto() {
int[] vxc = new int[VOTOS_X_CANDIDATO.length];
int[] clasificacion = new int[VOTOS_X_CANDIDATO.length];
for (int i = 0; i < clasificacion.length; i++) {
clasificacion[i] = i;
}
copiarArray(vxc, VOTOS_X_CANDIDATO);
int j;
for (int i = 1; i < vxc.length; i++) {
int votos = vxc[i];
int cand = clasificacion[i];
j = i;
while (j > 0 && votos < vxc[j - 1]) {
vxc[j] = vxc[j - 1];
clasificacion[j] = clasificacion[j - 1];
j--;
}
vxc[j] = votos;
clasificacion[j] = cand;
}
return clasificacion;
}

public String repetir(char ch, int cant) {

char[] chars = new char[cant];

for (int i = 0; i < cant; i++) {
chars[i] = ch;
}

return new String(chars);
}

public String alinear(int nro, int ancho, byte alineacion) {
return alinear(String.valueOf(nro), ancho, alineacion);
}

public String alinear(String texto, int ancho, byte alineacion) {

if (texto == null) {
return repetir(' ', ancho);
}

if (texto.length() > ancho) {
return texto.substring(0, ancho);
}

if (alineacion == DERECHA) {
return repetir(' ', ancho - texto.length()) + texto;
} else {
return texto + repetir(' ', ancho - texto.length());
}
}

public String centrar(String texto, int ancho) {
if (texto == null) {
return repetir(' ', ancho);
}
if (texto.length() >= ancho) {
return texto.substring(0, ancho);
}
String s = repetir(' ', (ancho - texto.length()) / 2) + texto;
return s + repetir(' ', ancho - s.length());
}

public static void main(String[] args) throws Exception {
Elecciones e = new Elecciones();
e.cargarDatos();
e.mostrar();
e.votosPorCandidato();
}
}
Muchisimas gracias de antemano !!!
  #2 (permalink)  
Antiguo 18/06/2014, 01:09
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 8 meses
Puntos: 188
Respuesta: Consulta, array multidimensional

plantéate que las condiciones para recuperar el candidato más votado y el ganador están al revés de como deberían estar.
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}

Etiquetas: multidimensional, programa, string
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 20:18.