-
Notifications
You must be signed in to change notification settings - Fork 0
/
popularVote.cpp
48 lines (45 loc) · 1.31 KB
/
popularVote.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int testCases;
cin >> testCases;
for (int i = 0; i < testCases; i++) {
int numCand;
int winner = -1, numVotes, totalVotes = 0;
vector<int>candVotes;
cin >> numCand;
for (int j = 0; j < numCand; j++) {
cin >> numVotes;
candVotes.push_back(numVotes);
totalVotes += numVotes;
for (int k = 0; k < candVotes.size(); k++) {
if (candVotes.at(k) == *max_element(candVotes.begin(), candVotes.end())) {
winner = k;
}
}
}
int winnerVotes = candVotes.at(winner);
candVotes.erase(candVotes.begin()+ winner);
bool multWin = false;
for (int j = 0; j < candVotes.size(); j++) {
if (candVotes.at(j) == winnerVotes) {
multWin = true;
}
}
if (multWin) {
cout << "no winner\n";
}
else {
if (static_cast<double>(totalVotes) / winnerVotes < 2) {
cout << "majority ";
}
else {
cout << "minority ";
}
cout << "winner " << winner + 1 << endl;
}
}
return 0;
}