【HDOJ】1225 Football Score
这种结构体排序的题,十分容易考上机题,qsort+结构体解决。马上就要机考了,多练习一下这样的题目也好。
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define MAXNUM 100 6 #define NAMENUM 20 7 #define WIN 3 8 #define DRAW 1 9 10 typedef struct { 11 char name[NAMENUM]; 12 int in, lost; 13 int score; 14 } team_st; 15 16 team_st teams[MAXNUM]; 17 int total; 18 19 int isExist(char name[], int n) { 20 int i; 21 for (i=0; i<n; ++i) 22 if (strcmp(teams[i].name, name) == 0) 23 return i; 24 25 return -1; 26 } 27 28 void check(char name[], int in, int lost) { 29 int index = isExist(name, total); 30 31 if (index == -1) { 32 strcpy(teams[total].name, name); 33 index = total; 34 total++; 35 } 36 teams[index].in += in; 37 teams[index].lost += lost; 38 if (in > lost) 39 teams[index].score += WIN; 40 if (in == lost) 41 teams[index].score += DRAW; 42 } 43 44 int comp(const void *a, const void *b) { 45 team_st *p1 = (team_st *)a; 46 team_st *p2 = (team_st *)b; 47 if (p1->score != p2->score) 48 return p2->score - p1->score; 49 else if ((p1->in-p1->lost) != (p2->in-p2->lost)) 50 return (p2->in-p2->lost) - ((p1->in-p1->lost)); 51 else if (p1->in != p2->in) 52 return p2->in - p1->in; 53 else 54 return strcmp(p1->name, p2->name); 55 } 56 57 int main() { 58 int n; 59 int i, a, b; 60 char stra[NAMENUM], strb[NAMENUM]; 61 62 while (scanf("%d", &n) != EOF) { 63 total = 0; 64 memset(teams, 0, sizeof(teams)); 65 for (i=1; i<=n*(n-1); ++i) { 66 getchar(); 67 scanf("%s VS %s %d:%d", stra, strb, &a, &b); 68 check(stra, a, b); 69 check(strb, b, a); 70 } 71 qsort(teams, total, sizeof(team_st), comp); 72 for (i=0; i<total; ++i) 73 printf("%s %d\n", teams[i].name, teams[i].score); 74 printf("\n"); 75 } 76 77 return 0; 78 }