EOJ 2878:百度面試字串排序
阿新 • • 發佈:2019-03-05
atoi getchar abcd sort clu for cmm 函數 continue
占坑:
#include <bits/stdc++.h> using namespace std; const int n=105; struct data { char str[40]; char num[10]; int x; }d[n]; bool strcmmp(char *a, char *b)//return 1: b is bigger, return 0: a is bigger { int lena=strlen(a), lenb=strlen(b); for (int i=0;i<min(lena,lenb);i++)if (a[i]<b[i]) return 1; else if (a[i]>b[i]) return 0; return lena<lenb; } bool cmp(data a, data b) { if(a.x==b.x) return strcmmp(a.str,b.str)==1; return a.x<b.x; } int main() { char ch; int i=0, j=0, k=0; while(~(ch=getchar())){if(ch==‘\n‘) continue; if(ch!=‘ ‘){ d[i].str[j++]=ch; if(ch>=‘0‘&&ch<=‘9‘) d[i].num[k++]=ch; } else { i++; j=0; k=0; } } i++; int len=i; for(int i=0;i<len;++i) d[i].x=-1; for(int i=0;i<len;++i) if(strlen(d[i].num)) d[i].x=atoi(d[i].num); sort(d,d+len,cmp); for(int i=0;i<len;++i) printf("%s ",d[i].str,d[i].x); return 0; } /* abc# abc1 abc10 abcd abc2 abc */
註意點:strcmp賊坑賊坑賊坑!也不要試圖用strcmp排字符數組(他是排字符串的!)最保險就手寫strcmmp函數:頭對齊,往後比
EOJ 2878:百度面試字串排序