This paper records a small experience of the author who was bitten by a string (actually his own dish) in the process of doing the title, hoping to help Xiaobai like me deepen his understanding of a certain aspect of the string.

# root of all evils

Xiaoming decides to apply for a new QQ number. The system randomly generates several numbers for him to choose. Xiao Ming's number selection principle is:

1. Select the number with the largest sum of all numbers.

2. If there are multiple numbers and the sum of the numbers is the same, select the number with the largest value.

Please write a program to help Xiao Ming choose a QQ number.

Enter description

The input data consists of two lines. The first line has an integer n, indicating that there are n numbers to be selected (0 < n < 100), and the second line has n positive integers, indicating each number to be selected, and the length of each number does not exceed 9 digits. Each number is separated by a space, and each number is different.

Output description

Output the number selected according to Xiao Ming's number selection principle.

sample input

5

10000 11111 22222 333 1234

sample output

2222

# Problem Description:

First of all, my idea is to use pair to store the sum of the numbers of a number and the number itself, and then sort it with sort to take the last element. You can think about what the problem will be. (hint, the second requirement of the topic is the number with the largest value)

This is the comparison function I wrote (sort itself is also compared in this way)

bool cmp(pair<int, string>a, pair<int, string>b) { if (a.first != b.first) return a.first < b.first; else return a.second < b.second; }

# Cause analysis:

See the problem?

If you can see it at a glance, you already know the concept of dictionary order and the comparison of string.

In else, we use the less than sign to compare string s, which is similar to the strcmp function in c language. Its comparison method is to start from the first letter, compare each character (ascii) backward in turn, and return a value once it is different (refer to baidu for details).

For example, the dictionary order of 98455987 and 67669249698455987 is larger, but in fact, it is smaller in value.

Therefore, the greater than sign and the less than sign in the string may not reflect the comparison of real values (in fact, I knew this before, but I didn't notice = - =)

# Solution:

Notice "not necessarily"? In fact, the dictionary order can also reflect the value under some conditions.

That is when two strings have the same length.

Therefore, length is a key quantity. The numbers here are long and short, so we can't compare the size of strings. Therefore, we can think that for strings of different lengths, their lengths should be compared first. With long length and many digits, the value is naturally larger.

So we can rewrite the comparison function like this

bool cmp(pair<int, string>a, pair<int, string>b) { if (a.first != b.first) return a.first < b.first; else if (a.second.length() != b.second.length()) return a.second.length() < b.second.length(); else return a.second < b.second; }

# summary

The greater than less than in a string is the dictionary order. Only when the two strings (pure numbers) have the same length can they reflect the real value size. Otherwise, the length must be considered.

# Code attached

#include <iostream> #include <cmath> #include <algorithm> #include <string> #include <set> using namespace std; bool cmp(pair<int, string>a, pair<int, string>b) { if (a.first != b.first) return a.first < b.first; else if (a.second.length() != b.second.length()) return a.second.length() < b.second.length(); else return a.second < b.second; } int main() { pair<int, string> s[1000]; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i].second; int sum = 0, ans = 0; for (int j = 0; j < s[i].second.length(); j++) { sum += (s[i].second[j] - '0'); } s[i].first = sum; } sort(s, s + n, cmp); // for (int i = 0; i < n; i++) // cout << s[i].second << endl; cout << s[n - 1].second; return 0; }