单点时限: 2.0 sec
内存限制: 256 MB
n (1<n≤100) 个字符指针组成一个数组,每个指针指向一个字符串。通过该数组对其元素所指的字符串按以下顺序排序:按字符串中第一个数字字符的升序排序(不包含数字字符的字符串排在最前面)。若数字字符相同,则按字符串本身值的升序排序。
#include<iostream>
#include<algorithm>
using namespace std;
struct g {
string s;
bool flag;
int num;
};
bool cmp(g a,g b) {
if(a.flag==b.flag) {
if(a.flag==false)
return a.s<b.s;
else {
if(a.num!=b.num)
return a.num<b.num;
return a.s<b.s;
}
}
return a.flag<b.flag;
}
int main() {
int t;
cin>>t;
for(int i = 0; i< t; i++) {
int n;
cin>>n;
g G[n];
for(int j = 0; j < n; j++) {
cin>>G[j].s;
G[j].flag=false;
int z=0;
while(!isdigit(G[j].s[z])&&z<G[j].s.size())z++;
if(z!=G[j].s.size()) {
G[j].flag=true;
G[j].num=G[j].s[z]-'0';
}
}
sort(G,G+n,cmp);
printf("case #%d:\n",i);
for(int j = 0; j < n; j++)
printf("%s%c",G[j].s.c_str(),j<n-1?' ':'\n');
}
return 0;
}