2682 Problem H: New Feature in Windows 7
题意:给出n个字符串,按要求排序,要求如下:
If two filenames are same before certain position including start, the
order should be decided by these conditions:
a) If both of them are alphabets, the result depends on the alphabet order.
b) If one is alphabet and the other is number, the number is smaller.
c) If both of them are numbers, the result depends on number values.
例:
思路:学习了某位大神的sort排序..........
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <string>
5 #include <algorithm>
6 #include <iostream>
7 using namespace std;
8 const int N=110;
9
10 string str[N];
11
12 int dig(char c){return c>='0'&&c<='9';}
13 int ler(char c){return !dig(c);}
14
15 int cmp(string a,string b){
16 int i,j,len_a,len_b;
17 for(i=0;a[i]&&b[i];i++){
18 if(dig(a[i])&&dig(b[i])){
19 for(len_a=0,j=i;dig(a[j]);j++) len_a++;
20 for(len_b=0,j=i;dig(b[j]);j++) len_b++;
21 if(len_a<len_b) return 1;
22 else if(len_a>len_b) return 0;
23 else{
24 for(int k=0;k<len_a;k++)
25 if(a[i+k]!=b[i+k]) return a[i+k]<b[i+k];
26 }
27 i=j;
28 }
29 else if(dig(a[i])&&ler(b[i])) return 1;
30 else if(ler(a[i])&&dig(b[i])) return 0;
31 else{
32 if(a[i]!=b[i]) return a[i]<b[i];
33 }
34 }
35 if(a[i]) return 0;
36 return 1;
37 }
38
39 int main(){
40
41 // freopen("data.in","r",stdin);
42 // freopen("data.out","w",stdout);
43
44 int n;
45 while(scanf("%d",&n),n){
46 for(int i=0;i<n;i++) cin>>str[i];
47 sort(str,str+n,cmp);
48 for(int i=0;i<n;i++) cout<<str[i]<<endl;
49 cout<<endl;
50 }
51 return 0;
52 }