Description
Football is an excellent game, but sometimes it is cruel. As you know, two football teams may have a tie after regular time and even addition time, but we have to judge who is the winner, so penalty comes.
Here are the rules taken from FIFA:
1. If, before both teams have taken five kicks, one has scored more goals than the other could score, even if it were to complete its five kicks, no more kicks are taken;
2. If, after both teams have taken five kicks, both have scored the same number of goals, or have not scored any goals, kicks continue to be taken in the same order until one team has scored a goal more than the other from the same number of kicks;
3. Each kick is taken by a different player and all eligible players must take a kick before any player can take a second kick;
4. After every player has taken a kick, they take the second kick in the same order as the first kick, and the same with third, fourth ... kick.
Now we have some kick results of the game, but unfortunately Doraemon wrote some additional results on the paper of kick results, which make the results become a 22 * 3 matrix. Now you have to judge who is the winner and what is the score.
Input
The first line gives the number of test cases T, (1 ≤ T ≤ 1000), after the first line followedT cases. In each case, the first 11 lines give the kick results of the home team, in each line, there are 3 words(each word is either "yes" or "no") which indicates the result of the player's first kick, second kick and third kick(of course, the game would end before take three turns, but Doraemon always makes things bad). The next 11 lines give the kick result of the away team. Home team takes the kick first, andplayer kicks in the given order.
Output
For each case, output contains three lines. The first line is "Match #:", where "#" is the case number. The second line is the "Winner:winner_name", (winner_name is either "home" or "away"). The third line is the final score "Score:home_score:away_score".
Sample Input
1 yes yes yes yes yes yes yes no no yes no no yes no no no no no no no no no no no no no no no no no no no no yes no no no no no no no no yes no no yes no no yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes
Sample Output
Match 1: Winner: home Score: 4:1
Hint
Doraemon suggests that you should use scanf to read data.
题目大意:就是模拟点球大战。方法就是模拟规则。
第一;如果两只队伍所罚的点球在五回合之内,则如果一方所得到的点球使得另一方即使在踢完5个回合都进的情况下的进球还要多,则该放获胜,比赛结束。
例如样例中:主队在踢完第四回合后已经近了4个球,而客队在踢第四回合之前才进1个球,即使剩下的两个回合全都踢进也才3个,故,在客队还没踢第四回合之前,比赛结束,比分为4:1
第二:如果双方在5个回合内(包括5)还没有决出胜负的话,之后,如果某一个回合结束完后,某方的进球数多与另一方,则该放胜利。
ps:情况没考虑清楚啊(只有第一轮才需要判断是否可以前五局内提前结束战斗,后面的两轮中,已经没有前五局提前结束的情况了),最后还是和作者程序对拍才弄明白的。。。切勿急躁
/*
@author : liuwen
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits> //INT_MAX INT_MIN LONG_LONG_MAX LONG_LONG_MIN
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int a[11][3],b[11][3];
char str[30];
int main()
{
//freopen("in2.txt","r",stdin);
//freopen("in.txt","r",stdin);
//freopen("out1.txt","w",stdout);
int T,i,j,cas=0;
scanf("%d",&T);
while(T--){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=0;i<11;i++){
for(j=0;j<3;j++){
scanf("%s",str);
if(strcmp(str,"yes")==0) a[i][j]=1;
else a[i][j]=0;
}
}
for(i=0;i<11;i++){
for(j=0;j<3;j++){
scanf("%s",str);
if(strcmp(str,"yes")==0) b[i][j]=1;
else b[i][j]=0;
}
}
bool isFirst=true; //是否为第一轮
int isFind=false; //是否已经结束
int sum1=0,sum2=0,flag; //sum1,sum2分别主队和客队的进球总数
for(j=0;j<3;j++){
if(isFirst){ //如果是第一轮,则需要判断能在5局内提前结束
isFirst=false;
for(i=0;i<5;i++){
sum1+=a[i][j]; //主队罚球
if(sum1>sum2+(5-i)) {flag = 1;isFind=true;break;} //如果主队能提前结束
else if(sum2>sum1+(4-i)){flag=2;isFind=true;break;} //如果客队能提前结束
sum2+=b[i][j]; //客队罚球
if(sum1>sum2+(4-i)) {flag = 1;isFind=true;break;} //如果主队能提前结束
else if(sum2>sum1+(4-i)) {flag = 2;isFind=true;break;}//客队提前结束
}
if(isFind) break; //是否提前结束
for(;i<11;i++){ //第一轮前五局还不能解决战斗
sum1+=a[i][j];
sum2+=b[i][j];
if(sum1>sum2){ //则每局结束后,进球多者获胜
flag=1;isFind=true;
break;
}else if(sum1<sum2){
flag=2;isFind=true;
break;
}
}
if(isFind) break;
}else{ //如果不是第一轮,则每局结束后,进球多者获胜
for(i=0;i<11;i++){
sum1+=a[i][j];
sum2+=b[i][j];
if(sum1>sum2){
flag=1;isFind=true;
break;
}else if(sum1<sum2){
flag=2;isFind=true;
break;
}
}
if(isFind) break;
}
}
printf("Match %d:\n",++cas);
printf("Winner: %s\n",flag==1 ? "home":"away");
printf("Score: %d:%d\n",sum1,sum2);
}
return 0;
}
下面的是作者给的代码,值得学习啊,用一维数组模拟二维,省了多少事,又不失可读性
#include <cstdio>
#include <cstring>
int main() {
freopen("in.txt","r",stdin);
freopen("out2.txt","w",stdout);
int re;
char buf[80];
int a[80], b[80], x, y;
scanf("%d", &re);
for (int ri = 1; ri <= re; ++ri) {
for (int i = 0; i < 11; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%s", buf);
a[j * 11 + i] = (buf[0] == 'y' ? 1 : 0);
}
}
for (int i = 0; i < 11; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%s", buf);
b[j * 11 + i] = (buf[0] == 'y' ? 1 : 0);
}
}
x = y = 0;
for (int i = 0; i < 11 * 3; ++i) {
if (i >= 5 && x != y) {
break;
}
x += a[i];
if (i < 5 && (x > y + 5 - i || y > x + 4 - i)) {
break;
}
y += b[i];
if (i < 5 && (y > x + 4 - i || x > y + 4 - i)) {
break;
}
}
printf("Match %d:\nWinner: %s\nScore: %d:%d\n", ri, (x > y) ? "home" : "away", x, y);
}
return 0;
}