您的位置:首页 > 其它

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: