【HDOJ】1225 Football Score
2014-03-22 14:27
190 查看
这种结构体排序的题,十分容易考上机题,qsort+结构体解决。马上就要机考了,多练习一下这样的题目也好。
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXNUM 100 #define NAMENUM 20 #define WIN 3 #define DRAW 1 typedef struct { char name[NAMENUM]; int in, lost; int score; } team_st; team_st teams[MAXNUM]; int total; int isExist(char name[], int n) { int i; for (i=0; i<n; ++i) if (strcmp(teams[i].name, name) == 0) return i; return -1; } void check(char name[], int in, int lost) { int index = isExist(name, total); if (index == -1) { strcpy(teams[total].name, name); index = total; total++; } teams[index].in += in; teams[index].lost += lost; if (in > lost) teams[index].score += WIN; if (in == lost) teams[index].score += DRAW; } int comp(const void *a, const void *b) { team_st *p1 = (team_st *)a; team_st *p2 = (team_st *)b; if (p1->score != p2->score) return p2->score - p1->score; else if ((p1->in-p1->lost) != (p2->in-p2->lost)) return (p2->in-p2->lost) - ((p1->in-p1->lost)); else if (p1->in != p2->in) return p2->in - p1->in; else return strcmp(p1->name, p2->name); } int main() { int n; int i, a, b; char stra[NAMENUM], strb[NAMENUM]; while (scanf("%d", &n) != EOF) { total = 0; memset(teams, 0, sizeof(teams)); for (i=1; i<=n*(n-1); ++i) { getchar(); scanf("%s VS %s %d:%d", stra, strb, &a, &b); check(stra, a, b); check(strb, b, a); } qsort(teams, total, sizeof(team_st), comp); for (i=0; i<total; ++i) printf("%s %d\n", teams[i].name, teams[i].score); printf("\n"); } return 0; }
相关文章推荐
- hdoj 1225 Football Score
- HDOJ 1225 Football Score(结构体排序)
- HDU 1225 Football Score
- hdoj 1225 football sorce(尝试了50次的水题)
- hdu 1225 Football Score
- HDU1225 Football Score 【结构体排序】
- 杭电 1225 Football Score
- 杭电1225 Football Score (结构体排序)
- HDU 1225 football score 结构体排序
- HDU-1225 Football Score 模拟问题(水题)
- hdu 1225 Football Score
- HDU-1225-Football Score
- HDU-1225-Football Score
- 【杭电oj】1225 - Football Score(结构体排序)
- HDU-1225-Football Score
- hdoj 1242 Rescue
- HDOJ 1233 还是畅通工程(并查集)
- HDOJ1213 How Many Tables(并查集)
- HDOJ 1004 Let the Balloon Rise
- hdoj 1091