Write a program to keep track of a match consisting of a series of games between two people: player A and player B, and report the outcome. The input consists of a sequence of letters A or B. If the input is A, it indicates that A has won a game. If it is B, then it indicates B has won a game. The first player to win 5 or more games with a difference of 2 or more games between him and his opponent wins the match. If no player wins the match in 20 games then the match is declared a tie after these 20 games have been played.
INPUT
### The next n lines contain a char value each
c0
c1
...
cn
n ≤ 20 . ci is the outcome of the ith game. It can take a value of either A/B. A indicates that this game was won by Player A and B indicates that it was won by Player B. The input will end when a player has won according to the given rules or 20 characters have been given.
OUTPUT
At the end of the input, if player A wins, output “ A”, If player B wins output “B”. If no one has won, output “Tie”
Note: DO NOT output the quotes
Please print a newline( using “cout<<endl;” is one way to do it) after printing your answer
#include <iostream>
using namespace std;
int main(){
char match;
int i,a,b;
a=0;
b=0;
for(i=1;i<=20;++i){
cout<<"Enter a if a has won or b if b has won"<<endl;
cin>>match;
if(match=='a'){
++a;
}
if(match=='b'){
++b;
}
if(a-b>=2 && a>=5){
cout<<"a won"<<" a="<<a<<" "<<"b="<<b<<endl;
break;
}
if(b-a>=2 && b>=5){
cout<<"b won b="<<b<<" a="<<a<<endl;
break;
}
if((a-b<2) && (i==20)|| (b-a<2 ) && (i==20)) {
cout<<"draw "<<" a="<<a<<" "<<"b="<<b<<endl;
break;
}
}
return 0;
}
Comments
Leave a comment