1100. Tennis Anyone?
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
When there's a big tennis tournament going on, many airline passengers want to know the latest news as soon as they get off their flights. Flight Information Television (FIT) has a group of in-airport televisions to display latest sports results, including tennis and want your help to display the results of tennis matches.
Each tennis match is divided into sets. Each set is divided into games. Each game is divided into points. Your task is to write a program that produces the score for each set and the name of the winner of a match, if there is one.
The rules of the game are as follows:
- Alternating Servers
At the beginning of the first game, one player shall be called the Server, and the other the Receiver. At the end of the first game the Receiver shall become Server, and the Server Receiver; and so on alternately in all the subsequent games. The Server’s score is always listed first in a score pair. - Score in a Game
If a player wins her first point, the score is called 15 for that player; on winning her
second point, the score is called 30 for that player; on winning her third point, the score is called 40 for that player, and the fourth point won by a player is called “game” for that player except as below:
If both players have won three points, the score is 40/40 which is called “deuce”. If the Server wins the next point, she has a scored the “advantage” and the score is given as “advantage-in” or “ad-in” for short. However, if the Receiver wins at “deuce” the advantage is given to the Receiver and score is called “advantage-out” or “ad-out” for short. If the same player wins the next point, she wins the game; if the other player wins the next point the score returns to “deuce”; and so on, until a player wins the two points immediately following the score of “deuce”. - Score in a Set
A player who first wins six games wins a set; except that she must win by a margin of two games over her opponent and if necessary a set is extended until this margin is achieved. - Maximum Number of Sets
The maximum number of sets in a match shall be 3. The winner of the match is the person that wins 2 sets.
Immediately after each point, the score of the game is called and the information about the games is sent to FIT.
Input
The input file will consist of one or more data sets, each representing a match. The first line of each data set contains the names of the players. There will be two strings of 1 to 80 letters, separated by at least one space. The first name on the line will be the Server for the first game. The next one or more lines of the data set contain the scores called out during a single game. One line will hold all the scores called during one game. These scores will either be a pair of numbers separated by white space (such as "0 0" or "40 15") or one of the words "ad-in", "ad-out", "deuce", or "game". All words in the score will be in lower case. No score will be given after a player has won, so each line will be terminated with the word "game". There will be at least one space between all scores on the score lines. The score lines for a game will end with a single line with the word "done". Each score line will represent a completed game, but the match, or even the set, may not be completed.
A line with the single word "quit" instead of the pair of names indicates the end of the input data.
You may assume all score lines represent valid games and there are never more score lines than are necessary for a match.
Output
The output for each match will consist of three lines. The first line will have the names of the two players in the same case and order listed in the input, separated by a single hyphen. The next line will have the number of games won by each player in each set separated by white space. For each set, have the games won by the first player listed first, a single hyphen, then the games won by the second player. Sets with a score of 0-0 should not be listed. The third line will either have the name of the player who won followed by the word "won" or the words "Match in progress" for a match that is not yet complete.
Print a blank line after the output for each data set.
Sample Input
ann mary 0 0 0 15 15 15 30 15 40 15 40 30 game 0 0 15 0 15 15 30 15 30 30 30 40 game 0 0 15 0 15 15 30 15 30 30 30 40 game 0 0 15 0 15 15 30 15 40 15 game 0 0 15 0 30 0 40 0 game 0 0 15 0 30 0 40 0 game 0 0 15 0 15 15 30 15 40 15 game 0 0 15 0 30 0 40 0 game 0 0 0 15 0 30 0 40 game 0 0 15 0 30 0 40 0 game 0 0 0 15 0 30 0 40 game 0 0 15 0 15 15 15 30 30 30 40 30 deuce ad-in game 0 0 15 0 30 0 40 0 game done Sara Nina 0 0 0 15 15 15 30 15 40 15 40 30 game 0 0 15 0 15 15 30 15 30 30 30 40 game 0 0 15 0 15 15 15 30 30 30 40 30 deuce ad-in deuce ad-in game 0 0 15 0 30 0 30 15 30 30 40 30 deuce ad-out game 0 0 0 15 0 30 15 30 15 40 30 40 deuce ad-out deuce ad-in game 0 0 15 0 15 15 30 15 30 30 30 40 game 0 0 15 0 15 15 30 15 40 15 game 0 0 15 0 30 0 40 0 game 0 0 15 0 30 0 40 0 game 0 0 15 0 15 15 30 15 40 15 game 0 0 15 0 30 0 40 0 game 0 0 0 15 0 30 0 40 game 0 0 15 0 30 0 40 0 game 0 0 0 15 0 30 0 40 game done quit
Sample Output
ann-mary 4-6 1-2 Match in progress Sara-Nina 6-0 6-2 Sara won
还是蛮凶残的模拟题,叫你模拟网球的计分,还好懂一点网球。。
#include <stdio.h>
#include <string.h>
int string_to_num(char point_char) { //将读到的字符串转为数字
if (point_char == '0') {
return 0;
} else if (point_char == '1') {
return 15;
} else if (point_char == '3') {
return 30;
} else if (point_char == '4') {
return 40;
}
}
int finish_set(int a, int b, int set_num) { //判断这一局是否打完了
if (a < 6 && b < 6)
return set_num;
else if (a - b >= 2 || b - a >= 2)
return set_num + 1;
else
return set_num;
}
void output(char a[], char b[], int ab[2][4], int now_set_num) { //输出第三行
if (now_set_num >= 3) { //now_set_num都到3了肯定打完了三局,必有胜者
int ap = 0, bp = 0;
for (int i = 0; i < 3; i++) {
if (ab[0][i] > ab[1][i])
ap++;
else
bp++;
}
if (ap > bp)
printf("%s won\n", a);
else
printf("%s won\n", b);
} else if (now_set_num <= 1) { //第二局还没打完必然没胜者
printf("Match in progress\n");
} else if (now_set_num == 2) { //打到第三局,判断前面两局的情况即可
int ap = 0, bp = 0;
for (int i = 0; i < 2; i++) {
if (ab[0][i] > ab[1][i])
ap++;
else
bp++;
}
if (ap == bp)
printf("Match in progress\n");
else if (ap == 0)
printf("%s won\n", b);
else if (bp == 0)
printf("%s won\n", a);
}
}
int main() {
char player_a[100], player_b[100];
while (scanf("%s", player_a) && strcmp(player_a, "quit")) {
bool server_is_a = true; //发球的人是不是a,刚开始是
scanf("%s", player_b);
char point_s[10], point_r[10]; //记录读入的分数(一对)
int point_s_game = 0, point_r_game = 0; //记录每个game的分数
int ab_point[2][4]; //分数记录器,注意要开到4,因为下面的输出第二行我的判断方法
int now_set_num = 0; //当前局数
memset(ab_point, 0, sizeof(ab_point));
while (scanf("%s", point_s) && strcmp(point_s, "done")) { // a match
if ('0' <= point_s[0] && point_s[0] <= '9') {
point_s_game = string_to_num(point_s[0]);
scanf("%s", point_r);
point_r_game = string_to_num(point_r[0]);
} else if (!strcmp(point_s, "deuce")) {
point_s_game = point_r_game = 40;
} else if (!strcmp(point_s, "ad-in")) {
point_s_game = 99; //其实这里写大于40的就行
} else if (!strcmp(point_s, "ad-out")) {
point_r_game = 99;
} else if (!strcmp(point_s, "game")) {
if ((point_s_game > point_r_game) ^ server_is_a) { //这里列个表看看 发球人得分多 和 发球人是a 的加分关系就清楚了
ab_point[1][now_set_num]++;
} else {
ab_point[0][now_set_num]++;
}
now_set_num = finish_set(ab_point[0][now_set_num], ab_point[1][now_set_num], now_set_num);
server_is_a = !server_is_a; //发球人轮替
}
}
if (!strcmp(point_s, "done")) {
printf("%s-%s\n", player_a, player_b);
for (int i = 0; ab_point[0][i] || ab_point[1][i]; i++) {
if (i != 0)
printf(" ");
printf("%d-%d", ab_point[0][i], ab_point[1][i]);
}
printf("\n");
output(player_a, player_b, ab_point, now_set_num);
printf("\n");
}
}
return 0;
}