题目链接
题目梗概
对大数字进行排序,输出最大值。
解题梗概
因为数字较大,有的达到了100位,所以采用字符串存储。这里给出的数字都是首部不包含0的,不必进行数据预处理。
比较的方式
- 首先位数多的数字比位数小的数字肯定更大一些,即字符串长度大的优先级大。
例如,123 > 12
- 其次位数相同时,高位较大的数优先级大
例如,121> 111
完整代码
#include <iostream>
#include <algorithm>
using namespace std;
struct candidate{
int no;
string ticket;
}c[21];
bool cmp(candidate a, candidate b){
string sa = a.ticket, sb = b.ticket;
if(sa.length() != sb.length()) return sa.length() > sb.length();
else{
for(int i = 0;i<sa.length();i++){
if(sa[i] == sb[i]) continue;
else return sa[i] > sb[i];
}
}
}
int main(){
int n;
cin >> n;
for(int i = 0;i<n;i++){
c[i].no = i + 1;
cin >> c[i].ticket;
}
sort(c, c+n, cmp);
cout << c[0].no << endl << c[0].ticket;
return 0;
}