【HDOJ】2093 考试排名
水题,考察结构体以及格式化输出,马上就要机试了,这道题挺好,留下来。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define MAXNUM 10005 6 7 typedef struct { 8 int ac; 9 int time; 10 char name[12]; 11 } student_st; 12 13 student_st students[MAXNUM]; 14 15 int comp(const void *a, const void *b) { 16 student_st *p1 = (student_st *)a; 17 student_st *p2 = (student_st *)b; 18 if (p1->ac != p2->ac) 19 return ( p1->ac < p2->ac ); 20 else if (p1->time != p2->time) 21 return ( p1->time > p2->time ); 22 else if ( strcmp(p1->name, p2->name) < 0 ) 23 return -1; 24 else 25 return 1; 26 } 27 28 int main() { 29 int i, j, k, index, tmp, extra; 30 int n, m; 31 char buf[12]; 32 33 scanf("%d %d", &n, &m); 34 getchar(); 35 36 index= 0; 37 while (scanf("%s", students[index].name) != EOF) { 38 students[index].ac = 0; 39 students[index].time = 0; 40 41 for (i=0; i<n; ++i) { 42 scanf("%s", buf); 43 44 if (buf[0] == '-' || buf[0] == '0') 45 continue; 46 students[index].ac++; 47 k = 0; 48 extra = 0; 49 tmp = 0; 50 for (j=0; j<strlen(buf); ++j) { 51 if (k && buf[j] != ')') { 52 extra = 10*extra + buf[j]-'0'; 53 } 54 if (buf[j] == '(') 55 k = 1; 56 if (k == 0) { 57 tmp = 10*tmp + buf[j]-'0'; 58 } 59 } 60 61 students[index].time += (tmp + extra*m); 62 } 63 64 // printf("%s: ac=%d, time=%d\n", students[index].name, students[index].ac, students[index].time); 65 getchar(); 66 ++index; 67 } 68 69 qsort(students, index, sizeof(student_st), comp); 70 71 for (i=0; i<index; ++i) 72 printf("%-10s %2d %4d\n", students[i].name, students[i].ac, students[i].time); 73 74 return 0; 75 }