P1781. 宇宙总统

考点

  • 排序

题解

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
#include <bits/stdc++.h>

using namespace std;

const int LEN = 21;

struct Node
{
int idx_;
string votes_;
} arr[LEN];

bool cmp(Node a, Node b)
{
if (a.votes_.length() != b.votes_.length())
return a.votes_.length() > b.votes_.length();
for (int i = 0; i < a.votes_.length(); ++i)
{
if (a.votes_[i] > b.votes_[i])
return true;
if (a.votes_[i] < b.votes_[i])
return false;
}
return true;
}

int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> arr[i].votes_;
arr[i].idx_ = i + 1;
}
sort(arr, arr + n, cmp);
cout << arr[0].idx_ << endl;
cout << arr[0].votes_;
return 0;
}

思路

本题实际上是比较以字符串形式存储的数字大小;先比较长度,更长的肯定更大,若长度相同再逐位比较