所以这是问题所在 在市议会选举中,有24名候选人竞选5个席位。得票最多的候选人成为城市监督员;接下来的4名候选人成为普通的市议会代表。剩下的19名候选人回到他们安静绝望的生活中。
附加到此保管箱后,您将找到一份明文投票文件,其中包含为24名候选人投票的大约19,000张选票。候选人的编号从1到24,每个选票用一行代表所选候选人的编号。
以下是候选人。 (请注意,为了这个例子,这些名称是唯一的;不要担心两个Harrisons或Cleveland的非连续条款。) 华盛顿 亚当斯 杰斐逊 麦迪逊 门罗 6.昆西亚当斯 杰克逊 范文伦 哈里森 泰勒 波尔克 12.泰勒 菲尔莫尔 皮尔斯 15.布坎南 16.林肯 17.约翰逊 18.格兰特 19.海耶斯 20.加菲猫 21.亚瑟 22.克利夫兰 23.麦金利 24.罗斯福
对于这个问题,请写一个
或者首先,以及其余4位获胜者,按照投票数的降序排列。
使用优先级队列来实现此程序。
确保您的计划能够妥善处理任意数量的理事会席位获胜者,任意数量的候选人和任何输入文件。即,将所有信息都包含在变量中,而不是硬编码。
虽然提供的投票文件具有“好”数据,但您的代码也应该为坏数据做好准备。考虑边缘情况,例如对不存在的候选人的投票,悬挂的乍得,那种事情。
这是我的代码
import java.io.*;
import java.util.PriorityQueue;
public class Election {
static int count1=0, count2=0, count3=0, count4=0, count5=0,count6=0,count7=0,count8=0,count9=0,count10=0,count11=0,count12=0,count13=0,count14=0,count15=0,count16=0,count17=0,count18=0,count19=0,count20=0,count21=0,count22=0,count23=0,count24=0;
static String line;
public static void main (String [] args) throws IOException
{
PriorityQueue<Integer> listpeople = new PriorityQueue<Integer>();
FileReader file = new FileReader("C:\\votes.txt");
BufferedReader in = new BufferedReader(file);
while ((line = in.readLine()) != null)
{
int a = Integer.parseInt(line);
listpeople.add(a);
}
in.close();
for (int i = 0; i <= listpeople.size(); i++)
{
if (listpeople.poll() == 1)
{
count1++;
}
else if(listpeople.poll() == 2)
{
count2++;
}
else if (listpeople.poll() == 3)
{
count3++;
}
else if (listpeople.poll() == 4)
{
count4++;
}
else if (listpeople.poll() == 5)
{
count5++;
}
else if (listpeople.poll() == 6)
{
count6++;
}
else if (listpeople.poll() == 7)
{
count7++;
}
else if (listpeople.poll() == 8)
{
count8++;
}
else if (listpeople.poll() == 9)
{
count9++;
}
else if (listpeople.poll() == 10)
{
count10++;
}
else if (listpeople.poll() == 11)
{
count11++;
}
else if (listpeople.poll() == 12)
{
count12++;
}
else if (listpeople.poll() == 13)
{
count13++;
}
else if (listpeople.poll() == 14)
{
count14++;
}
else if (listpeople.poll() == 15)
{
count15++;
}
else if (listpeople.poll() == 16)
{
count16++;
}
else if (listpeople.poll() == 17)
{
count17++;
}
else if (listpeople.poll() == 18)
{
count18++;
}
else if (listpeople.poll() == 19)
{
count19++;
}
else if (listpeople.poll() == 20)
{
count20++;
}
else if (listpeople.poll() == 21)
{
count21++;
}
else if (listpeople.poll() == 22)
{
count22++;
}
else if (listpeople.poll() == 23)
{
count23++;
}
else if (listpeople.poll() == 24)
{
count24++;
}
}
int [] anArray = new int [24];
anArray[0] = count1;
anArray[1] = count2;
anArray[2] = count3;
anArray[3] = count4;
anArray[4] = count5;
anArray[5] = count6;
anArray[6] = count7;
anArray[7] = count8;
anArray[8] = count9;
for(int i =0; i<9; i++)
{
System.out.println(anArray[i]);
}
}
}
答案 0 :(得分:0)
试试这个:
public class Election {
static String line;
public static int highestLocation(int[] x){
int max = 0, value = 0;
for (int i=0; i<x.length; i++){
if (x[i] > max){
max = x[i];
value = i;
}
}
return value;
}
public static void main(String[] args) throws IOException {
PriorityQueue<Integer> listpeople = new PriorityQueue<Integer>();
FileReader file = new FileReader("C:\\votes.txt");
BufferedReader in = new BufferedReader(file);
while ((line = in.readLine()) != null) {
int a = Integer.parseInt(line);
listpeople.add(a);
}
in.close();
int[] candidates = new int[24];
for (int i = 0; i <= listpeople.size(); i++) {
int x = listpeople.poll();
candidates[x-1]++;
}
for (int i = 0; i < 9; i++) {
int next = highestLocation(candidates);
System.out.println((next+1) + " " + candidates[next]);
candidates[next] = 0;
}
}
}
您的数据集的输出:
8 2730
1 1669
6 1653
2 1495
5 1474
9 112
3 103
10 77
7 44