1018. 锤子剪刀布 (20)
2015-09-08 19:52
363 查看
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
输出格式:
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。
输入样例:
输出样例:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第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; }
相关文章推荐
- 促进胎儿入盆试试运动法和调整姿势
- 胎儿入盆是什么感觉 怎样促进胎儿入盆
- HTTP - 条件请求
- Markdown编辑器写博客
- 第2周-跑道长度
- 浅析SAE与DBM(Deep Learning)
- OC 字符串及数组
- Vim 配色设置与配色脚本语法
- JAXB--学习2
- [Swoole入门]-Ubuntu环境下安装Swoole扩展
- ArcGIS Server 10.1下载地址
- UVA 10817(状态压缩)
- iOS7 boundingRectWithSize 计算文本尺寸
- OGRE学习记录-自阴影
- SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'dtdate' 解决方法
- 交换两个int类型数值(总结分析,理解记忆)
- c#的托管事件
- Mantis实验提高篇
- iOS 异步请求网络数据,传值问题
- Codeforces 55D Beautiful numbers 数位dp 数论 优化技巧