日志排序
日志排序
http://www.nowcoder.com/questionTerminal/0f64518fea254c0187ccf0ea05019672
看到大佬的代码学会了sscanf
#include<stdio.h> #include <stdlib.h> #include<string.h> typedef struct{ char s[100]; char name[20]; char start1[20]; char start2[20]; double time; }Node; int cmp(const void *a,const void *b) { Node * x = (Node *)a; Node * y = (Node *)b; if(x->time != y->time) return x->time*1000 - y->time*1000; else if(strcmp(x->start1,y->start1)) return strcmp(x->start1,y->start1); else return strcmp(x->start2,y->start2); } int main() { Node n[10001]; int i = 0; while(gets(n[i].s) && strlen(n[i].s)) { sscanf(n[i].s,"%s%s%s%lf",n[i].name,n[i].start1,n[i].start2,&n[i].time); i++; } qsort(n,i,sizeof(Node),cmp); for(int j = 0;j<i;j++) printf("%s\n",n[j].s); return 0; }