您的位置:首页 > 其它

1018. 锤子剪刀布 (20)

2015-09-08 19:52 363 查看
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:



现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式:

输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。

输出格式:

输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。
输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

输出样例:
5 3 2
2 3 5
B B

//用一个map将包剪锤转换成数字,然后相减,如果小于0就加3,得到的数字就是比赛结果,最后用一个对相统计
//用cout会超时

#include <iostream>
#include <map>
#include <stdio.h>

using namespace std;

map<char, int> name_to_num;
const char num_to_name[] = { 'B', 'C', 'J'};

class Player{
private:
int times[3] ;
int games[3];
public:
Player(){
for (int i = 0; i < 3; i++){
times[i] = 0;
games[i] = 0;
}
};
void win(int action){
games[0] ++;
times[action] ++;
}
void lose(){
games[2]++;
}
void draw(){
games[1]++;
}
void printRes(){
for (int i = 0; i < 3; ++i){
if (i == 2){
printf("%d\n", games[i]);
//cout << games[i]<<endl;
}
else{
printf("%d ", games[i]);
//cout << games[i] << " ";
}
}
}
char mostTimes(){
int maxTime = 0;
int maxPos = 0;
for (int i = 0; i < 3; ++i){
if (times[i] > maxTime){
maxTime = times[i];
maxPos = i;
}
}
return num_to_name[maxPos];
}

};

int main(){

name_to_num.insert(pair<char, int>('B',0));
name_to_num.insert(pair<char, int>('C', 1));
name_to_num.insert(pair<char, int>('J', 2));
Player playerA, playerB;

int n; cin >> n;
char a, b;
while (n--){
//scanf("%c %c", &a, &b);
cin >> a >> b;
int tmp = name_to_num[a] - name_to_num[b];
if (tmp < 0){
tmp += 3;
}
if (tmp == 0){
playerA.draw();
playerB.draw();
}
else if (tmp == 1){
playerA.lose();
playerB.win(name_to_num[b]);
}
else if (tmp == 2){
playerA.win(name_to_num[a]);
playerB.lose();
}
}

playerA.printRes();
playerB.printRes();
printf("%c %c", playerA.mostTimes(), playerB.mostTimes());
//cout << playerA.mostTimes() << " " << playerB.mostTimes()<<endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: