1018 锤子剪刀布

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式

输入第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

idea

  • 注意用getchar()吸收空格
  • 测试点2,3不过的可能原因:
    “如果解不唯一,则输出按字母序最小的解”

solution1(直观解

#include <stdio.h>
char max(int a, int b, int c){
	if(a < b){
		if(b < c)
			return 'J';
		else
			return 'C';
	}
	else{
		if(a < c)
			return 'J';
		else
			return 'B';
	}
}
int main(){
	int n, ac = 0, aj = 0, ab = 0, a[3], bc = 0, bj = 0, bb = 0, b[3];
	char ca, cb;
	scanf("%d", &n);
	for(int i = 0; i < 3; i++){
		a[i] = 0;
		b[i] = 0;
	}
	for(int i = 0; i < n; i++){
		getchar();
		scanf("%c %c", &ca, &cb);
		if(ca == cb){
			a[1]++;
			b[1]++;
		}
		else{
			if(ca == 'J'){
				if(cb == 'C'){
					a[2]++;
					b[0]++;
					bc++;
				}
				else{
					a[0]++;
					b[2]++;
					aj++;
				}
			}
			else if(ca == 'B'){
				if(cb == 'J'){
					a[2]++;
					b[0]++;
					bj++;
				}
				else{
					a[0]++;
					b[2]++;
					ab++;
				}
			}
			else{
				if(cb == 'B'){
					a[2]++;
					b[0]++;
					bb++;
				}
				else{
					a[0]++;
					b[2]++;
					ac++;
				}
			}
		}
	}	
	printf("%d %d %d\n%d %d %d\n%c %c", a[0], a[1], a[2], b[0], b[1], b[2], max(ab, ac, aj), max(bb, bc, bj));
	return 0; 
}

solution(优化输赢判断

#include <stdio.h>
int change(char c){
	if(c == 'B') return 0;
	else if(c == 'C') return 1;
	else if(c == 'J') return 2;
}
char max(int a, int b, int c){
	if(a < b){
		if(b < c) return 'J';
		else return 'C';
	}
	else{
		if(a < c) return 'J';
		else return 'B';
	}
}
int main(){
	int n, j, y, a = 0, b = 0, c = 0, s1[3] = {0}, s2[3] = {0};
	char cj, cy;
	scanf("%d", &n);
	while(n--){
		getchar();
		scanf("%c %c", &cj, &cy);
		j = change(cj);
		y = change(cy);
		if(j == y) b++;
		else if((j + 1) % 3 == y){
			a++;
			s1[j]++;
		}
		else{
			c++;
			s2[y]++;
		}
	}
	printf("%d %d %d\n%d %d %d\n%c %c", a, b, c, c, b, a, max(s1[0], s1[1], s1[2]), max(s2[0], s2[1], s2[2]));
	return 0;
}
posted @ 2022-01-04 20:15  Moliay  阅读(15)  评论(0编辑  收藏  举报