题链接:https://www.luogu.com.cn/problem/AT_past202012_d帮帮忙
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
string s[100003];
bool strsort(string x, string y){
int rstate = 0; // x 与 y 谁位数长
if(x.size() > y.size()){
int msize = x.size() - y.size(); // 一定要预先存好差值!!!写在循环里值不是一定的,会 WA
for(int i = 0; i < msize; ++ i){ // 补 y
y = '0' + y;
}
rstate = 1;
}
else if(y.size() > x.size()){
int msize = y.size() - x.size();
for(int i = 0; i < y.size() - x.size(); ++ i){ // 补 x
x = '0' + x;
}
}
if(x != y){ // 不相等
return x < y; // 按字典序排,相当于按照大小
}
else{
return rstate; // 按初值的大小排,也相当于看前导 0 数量
}
}
int main(){
cin >> n;
for(int i = 1; i < n + 1; ++ i){
cin >> s[i];
}
stable_sort(s + 1, s + n + 1, strsort); // 推荐稳定排序
for(int i = 1; i < n + 1; ++ i){
cout << s[i] << '\n';
}
return 0;
}