這道題讀懂題意後還是很簡單的,除了中間要注意gets()函數的用法。gets()函數可接收空格符,並且以回車結束後會吸收掉結束的換行符。但是scanf()函數以空格和換行作為輸入的結束符,且不會吸收結束符。所以gets()前如果有scanf()函數,一定得加getchar()函數吸收掉sanf()函數的結束符。 這道題提交了15次才通過,很讓人糾結。注意:1、規則最後一條字典順序不分大小寫;2、兩隊的進球數是小於20,如果你用字符串直接接收每場比賽的結果,然後再進行處理要特別注意了;3、輸出文件最後不能有換行。 [cpp] #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++) { www.2cto.com 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; }