本文主要是介绍10194 - Football (aka Soccer),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:10194 - Football (aka Soccer)
题目大意:给你一些比赛记录,然后让你依据要求这些排序:
- Most points earned. (得分胜利3,平局1,失败0)
- Most wins.
- Most goal difference (i.e. goals scored - goals against)
- Most goals scored.
- Less games played. (比赛次数越少越好)
- Lexicographic order. (不区分大小写排序)
- 解题思路:就是按照这些要求写比较函数,用sort排序,然后输出即可。注意:scanf()是不舍弃回车,需要ch = getchar()接受回车,而gets或getline是可以舍弃回车的。然后要细心。数组不要越界,越界了不仅可能RE还有可能WA。两个数据组之间要有一个空白行。
#include<stdio.h> #include<string> #include<string.h> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std;const int N = 105; const int T = 35; const int M = 1005;char tourname[N], team[T][T], record[M][N]; int n, t, g;class RANK{public:RANK () { strcpy(name, ""); totalp = gameplay = win = tie = loss = different = score = against = 0;}char name[T];int totalp;int gameplay;int win; int tie;int loss;int different;int score;int against;void clear() {strcpy(name, ""); totalp = gameplay = win = tie = loss = different = score = against = 0;}} rank[T];void change(char *s) {for(int i = 0; i < strlen(s); i++) {if(s[i] >= 'A' && s[i] <= 'Z')s[i] += 32;} }bool cmp(const RANK &a, const RANK &b) {if(a.totalp > b.totalp)return true;else if(a.totalp < b.totalp)return false;if(a.win > b.win)return true;else if(a.win < b.win)return false;if (a.different > b.different)return true;else if(a.different < b.different)return false;if(a.score > b.score)return true;else if(a.score < b.score)return false;if(a.gameplay < b.gameplay)return true;else if(a.gameplay > b.gameplay)return false;char str1[T], str2[T];strcpy(str1, a.name);strcpy(str2, b.name);change(str1);change(str2);if(strcmp(str1, str2) < 0)return true;elsereturn false; }void ranked () {int i, j, k, l, bo;for( i = 0; i < g; i++) {char nam1[T],nam2[T], s1[5], s2[5];k = bo = 0;for( j = 0; j < strlen(record[i]); j++) {if(!bo && record[i][j] != '#')nam1[k++] = record[i][j];else if(!bo){nam1[k] = '\0';k = 0;bo = 1;j++;}if(bo && record[i][j] != '@')s1[k++] = record[i][j];else if(bo){s1[k] = '\0';k = 0;break;}}l = bo = 0;for(k = j + 1; k < strlen(record[i]); k++) {if(!bo &&record[i][k] != '#')s2[l++] = record[i][k];else if(!bo){s2[l] = '\0';l = 0;bo = 1;}if(bo && record[i][k] != '#') nam2[l++] = record[i][k];}nam2[l] = '\0';int a, b;a = atoi(s1);b = atoi(s2);for(j = 0; j < t; j++) {if(strcmp(rank[j].name, nam1) == 0) {rank[j].gameplay++;rank[j].score += a;rank[j].against += b;if(a > b) {rank[j].win++;rank[j].totalp += 3;}else if(a < b)rank[j].loss++;else {rank[j].totalp += 1;rank[j].tie++;}break;}}for(j = 0; j < t; j++) {if(strcmp(rank[j].name, nam2) == 0) {rank[j].gameplay++;rank[j].score += b;rank[j].against += a;if(a < b) {rank[j].win++;rank[j].totalp += 3;}else if(a > b) rank[j].loss++;else {rank[j].totalp += 1;rank[j].tie++;}break;}}}for (i = 0; i < t; i++) rank[i].different = rank[i].score - rank[i].against; }int main() {int i;char ch;scanf("%d", &n);ch = getchar();while (n--) {cin.getline(tourname, N);scanf("%d", &t);ch = getchar();for (i = 0; i < t; i++) {cin.getline(team[i], T);}scanf("%d", &g);ch = getchar();for (i = 0; i < g; i++) {cin.getline(record[i], N);}for(i = 0; i < t; i++)strcpy(rank[i].name, team[i]);ranked();sort(rank, rank + t, cmp);printf("%s\n",tourname);for(i = 0; i < t; i++) printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", i+1, rank[i].name, rank[i].totalp, rank[i].gameplay, rank[i].win, rank[i].tie, rank[i].loss, rank[i].different, rank[i].score, rank[i].against);if(n)printf("\n");memset(team, 0, sizeof(team));memset(record, 0, sizeof(record));for(i = 0; i < t; i++)rank[i].clear();}return 0; }
这篇关于10194 - Football (aka Soccer)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!