这道题读懂题意后还是很简单的,除了中间要注意gets()函数的用法。gets()函数可接收空格符,并且以回车结束后会吸收掉结束的换行符。但是scanf()函数以空格和换行作为输入的结束符,且不会吸收结束符。所以gets()前如果有scanf()函数,一定得加getchar()函数吸收掉sanf()函数的结束符。
这道题提交了15次才通过,很让人纠结。注意:1、规则最后一条字典顺序不分大小写;2、两队的进球数是小于20,如果你用字符串直接接收每场比赛的结果,然后再进行处理要特别注意了;3、输出文件最后不能有换行。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Team { char name[50]; int b, c, d, e, f, g, h, i; }team[50]; int n, t, g; int find(char str[]) { for (int i=0; i<t; i++) if (0 == strcmp(team[i].name, str)) return i; return 0; } // 将字符串大写转小写 void UTL(char str[]) { int len = strlen(str); for (int i=0; i<len; i++) if (str[i]>='A' && str[i] <= 'Z') { str[i] += ('a'-'A'); } } int cmp(const void *_a, const void *_b) { struct Team *a = (struct Team*)_a; struct Team *b = (struct Team*)_b; if (a->b != b->b) return b->b - a->b; if (a->d != b->d) return b->d - a->d; if (a->g != b->g) return b->g - a->g; if (a->h != b->h) return b->h - a->h; if (a->c != b->c) return a->c - b->c; char ta[50], tb[50]; strcpy(ta, a->name); strcpy(tb, b->name); UTL(ta); UTL(tb); return strcmp(ta, tb); } int main() { char tName[105]; scanf("%d", &n); getchar(); while (n--) { gets(tName); // gets()函数会把最后的回车吸收掉scanf却不会 memset(team, 0, sizeof (team)); scanf("%d", &t); getchar(); for (int i=0; i<t; i++) gets(team[i].name); scanf("%d", &g); getchar(); while (g--) { char ch, a[50], b[50]; int x = 0, w, l; while (ch = getchar()) { if ('#' == ch) break; a[x] = ch; x++; } a[x] = '\0'; scanf("%d", &w); // 一定要注意分数是小于20 getchar(); scanf("%d", &l); getchar(); gets(b); // 找到队名的编号 int aa, bb; aa = find(a); bb = find(b); team[aa].c += 1; team[bb].c += 1; team[aa].h += w; team[bb].h += l; team[aa].i += l; team[bb].i += w; team[aa].g = team[aa].h - team[aa].i; team[bb].g = team[bb].h - team[bb].i; if (w > l) { team[aa].b += 3; team[aa].d += 1; team[bb].f += 1; } else if (w == l) { team[aa].b += 1; team[bb].b += 1; team[aa].e += 1; team[bb].e += 1; } else { team[bb].b += 3; team[bb].d += 1; team[aa].f += 1; } } qsort(team, t, sizeof (team[0]), cmp); printf("%s\n", tName); for (int i=0; i<t; i++) { printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", i+1, team[i].name, team[i].b, team[i].c, team[i].d, team[i].e, team[i].f, team[i].g, team[i].h, team[i].i); } if (n > 0) // 还要注意最后不能有空行 printf("\n"); } return 0; }