Problemario de Programación
Arreglos
Ejercicio 5.36
Se tienen los resultados de las últimas elecciones a gobernador en el estado X, el cual está conformado por 5 municipios.
En dichas elecciones hubo 4 candidatos.
Elabore un programa que:
• Lea e imprima una tabla indicando los votos obtenidos en cada municipio por los 4 candidatos.
• Calcule el total de votos recibidos por cada candidato y el porcentaje del total de votos emitidos.
• Calcule el candidato más votado.
• Si un candidato recibió más del 50% de los votos, indicar que es el ganador. Si ningún candidato recibió más del 50% de los votos, el programa debe imprimir los dos candidatos más votados, que serán los que pasen a la segunda ronda de las elecciones.
package programacionPares;
public class _36_Ejercicio {
public static final int f = 5, c = 4;
public static void main(String[] args) {
int Matriz[][] = new int [f][c];
System.out.println("\t\tCand-1\tCand-2\tCand-3\tCand-4");
for (int i = 0; i < f; i++){
System.out.print("Municipio: ["+(i+1)+"]");
for (int j = 0; j < c; j++){
Matriz[i][j] = Integer.parseInt(javax.swing.JOptionPane.showInputDialog("Candidato ["+(j+1)+"]"+"\nMunicipio ["+(i+1)+"]", ""));
System.out.print("\t"+Matriz[i][j]);
}
System.out.println();
}
int Vector [] = new int [c];
// SUMA DE VOTOS
Vector = ResVect(Vector, Matriz);
System.out.println("\nCalculo de Candidatos");
System.out.print("Votos Total: ");
int totalVotos = 0;
for (int value: Vector){
System.out.print("\t"+value);
totalVotos += value;
}
double [] Porcentajes = new double [c];
// OBTIENE LOS PORCENTAJES
Porcentajes = ObtenerPorcent(totalVotos, Vector, Porcentajes);
System.out.println();
// RESULTADO DE VOTACION
ResultadosVotacion(Porcentajes);
}
// SUMA DE VOTACIONES
public static int [] ResVect (int [] Vec, int [][] Matriz){
int Suma, mayorVotado = 0, Pos = 0;
for (int j = 0; j < c; j++){
Suma = 0;
for (int i = 0; i < f; i++){
Suma += Matriz[i][j];
}
Vec[j] = Suma;
if (mayorVotado < Vec[j]){
mayorVotado = Vec[j];
Pos = j + 1;
}
}
return Vec;
}
// OBTIENE PORCENTAJES
public static double [] ObtenerPorcent(int totVotos, int [] aVect, double [] Porcentajes){
System.out.print("\nPorcentaje: ");
for (int i = 0; i < Porcentajes.length; i++){
Porcentajes[i] = (aVect[i]*100) / totVotos;
System.out.print("\t"+Porcentajes[i]+"%");
}
return Porcentajes;
}
// RESULTADOS DE VOTACIONES
public static void ResultadosVotacion(double[] porcentajes){
boolean verif = true;
double Mayor1 = 0;
int posMay1 = 0;
double Mayor2 = 0;
int posMay2 = 0;
for (int i = 0; i < porcentajes.length; i++){
if (porcentajes[i] > Mayor1){
Mayor1 = porcentajes[i];
posMay1 = i;
}
if (porcentajes[i] > 50.00){
System.out.println("GANADOR"+" Candidato "+(i+1));
verif = false;
}
}
for (int i = 0; i < porcentajes.length; i++){
if (posMay1 != i){
if (porcentajes[i] > Mayor2){
Mayor2 = porcentajes[i];
posMay2 = i + 1;
}
}
}
if (verif){
System.out.println("\nSEGUNDA VUELTA");
System.out.println("Candidato : "+(posMay1+1)+" porcentaje "+Mayor1);
System.out.println("Candidato : "+posMay2+" porcentaje "+Mayor2);
}
}
}
Captura de Ingreso de Matriz
Resultado 1 Ganador
Resultado 2 Segunda vuelta