#include <algorithm>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
int subtract(string s1,string s2){
if(s1[0]=='-'&&s2[0]!='-') return -1; //s1比s2小
else if(s2[0]=='-'&&s1[0]!='-') return 1; //s2比s1小
else {
bool flag =false; //s1的绝对值比s2小则为true
if(s1.length()!=s2.length()){ //位数不等
if(s1.length()>s2.length()) flag=true;
}else { //位数相等
if(strcmp(s1.c_str(),s2.c_str())>0) flag=true;
}
if(s1[0]=='-'&&s2[0]=='-'){
if(flag) return -1; //都是负数,s1的绝对值更大,则 s1-s2<0
else return 1;
}else{
if(flag) return 1; //都是正数,s1的绝对值更大
else return -1;
}
}
}
bool cmp(string s1, string s2){
return subtract(s1,s2)<0;
}
int main(){
int n;
cin>>n;
vector<string>s(n);
for(int i=0;i<n;i++)
cin>>s[i];
sort(s.begin(),s.end(),cmp);
for(int i=0;i<n;i++){
if(i==0) cout<<s[i];
else cout<<" "<<s[i];
}
return 0;
}
C: Easy Sort
最新推荐文章于 2024-11-12 19:41:23 发布