欢迎光临
我们一直在努力

P1781 宇宙总统

P1781 宇宙总统

题目描述

地球历公元 6036 年,全宇宙准备竞选一个最贤能的人当总统,共有

n

n

n 个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统。

输入格式

第一行为一个整数

n

n

n,代表竞选总统的人数。

接下来有

n

n

n 行,分别为第一个候选人到第

n

n

n 个候选人的票数。

输出格式

共两行,第一行是一个整数

m

m

m,为当上总统的人的号数。

第二行是当上总统的人的票数。

输入输出样例 #1

输入 #1

5
98765
12365
87954
1022356
985678

输出 #1

4
1022356

说明/提示

1

≤

n

≤

20

1 \\leq n \\leq 20

1≤n≤20。

票数可能会很大,可能会到

100

100

100 位数字。数据保证不存在票数相同的候选人。

解析

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
// 由于数据稍微有点大, 所以采用高精度
class Candidate{
private:
vector<int> digits;
public:
int id;
// 这里再写一个比较重载差不多了
bool operator>(const Candidate &bigNum) const{
if(digits.size() < bigNum.digits.size()) return false;
if(digits.size() > bigNum.digits.size()) return true;
// 接下来就是数位相等的情况, 从高位开始比较
for(int i = digits.size() – 1; i >= 0; i—){
if(digits[i] < bigNum.digits[i]) return false;
if(digits[i] > bigNum.digits[i]) return true;
}
// 这里说明两者完全相等
return false;
}
// 输出运算符重载
friend ostream& operator<<(ostream &os, const Candidate &c);
// 输入运算符重载
friend istream& operator>>(istream &is, Candidate &c);
};
ostream& operator<<(ostream &os, const Candidate &c){
for(auto it = c.digits.rbegin(); it != c.digits.rend(); it++)
os << *it;
return os;
}

istream & operator>>(istream &is, Candidate &c) {
string s;
is >> s;
for (auto it = s.rbegin(); it != s.rend(); it++)
c.digits.push_back(*it – '0');
return is;
}

int n;
vector<Candidate> candidates;

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
candidates.resize(n + 1);
for(int i = 1; i <= n; i++){
cin >> candidates[i];
candidates[i].id = i;
}
sort(candidates.begin(), candidates.end(), greater<Candidate>());
cout << candidates[0].id << '\\n' << candidates[0];
return 0;
}

赞(0)
未经允许不得转载:171主机测评 » P1781 宇宙总统
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址