aboutsummaryrefslogtreecommitdiff
path: root/algorithm-experiment/5.2b.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-11-19 11:57:35 +0800
committercrupest <crupest@outlook.com>2021-11-19 11:57:35 +0800
commitf1c97716c363f1095520b8ddd9c83cab6e339f54 (patch)
tree087bc8a680951f78c6be3849b24b7977d2cd307b /algorithm-experiment/5.2b.cpp
parenta80df838667eb5015b83e93169c6cad197c61aa4 (diff)
downloadlife-f1c97716c363f1095520b8ddd9c83cab6e339f54.tar.gz
life-f1c97716c363f1095520b8ddd9c83cab6e339f54.tar.bz2
life-f1c97716c363f1095520b8ddd9c83cab6e339f54.zip
Add algorithm experiment 5.
Diffstat (limited to 'algorithm-experiment/5.2b.cpp')
-rw-r--r--algorithm-experiment/5.2b.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/algorithm-experiment/5.2b.cpp b/algorithm-experiment/5.2b.cpp
new file mode 100644
index 0000000..d062ae0
--- /dev/null
+++ b/algorithm-experiment/5.2b.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+#include <unordered_map>
+#include <vector>
+
+int main() {
+ std::vector<long long> votes;
+
+ long long v;
+ while (std::cin >> v) {
+ votes.push_back(v);
+ }
+
+ std::unordered_map<long long, long long> counts;
+ for (auto v : votes) {
+ counts[v]++;
+ }
+
+ // Get ids with votes greater than half of the total votes.
+ long long half_votes = votes.size() / 2;
+
+ for (auto it = counts.begin(); it != counts.end(); ++it) {
+ if (it->second > half_votes) {
+ std::cout << it->first << std::endl;
+ }
+ }
+
+ return 0;
+}