1. 程式人生 > >PAT乙級題解(1018. 錘子剪刀布)

PAT乙級題解(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
這題賊煩(服氣臉)
#include <iostream>
using namespace std;
#define C 0
#define J 1
#define B 2

int wintimea[3] = { 0 };
int wintimeb[3] = { 0 };

int judge(char a, char b)//判決函式 
{
	int judge;
	if (a == 'C' && b == 'B') {
		judge = -1;//b用B贏了 
		wintimeb[B]++;
	}
	else if (a == 'B' && b == 'J') {
		judge = -1;//b用J贏了
		wintimeb[J]++;
	}
	else if (a == 'J' && b == 'C') {
		judge = -1;//b用C贏了 
		wintimeb[C]++;
	}
	else if (a == 'B' && b == 'C') {
		judge = 1;//a用B贏了 
		wintimea[B]++;
	}
	else if (a == 'J' && b == 'B') {
		judge = 1;//a用J贏了
		wintimea[J]++;
	}
	else if (a == 'C' && b == 'J') {
		judge = 1;//a用C贏了
		wintimea[C]++;
	}
	else if (a == b) {
		judge = 0;
	}
	return judge;
}

int main()
{
	int n;
	int result;
	cin >> n;
	char a, b;
	int wina = 0, winb = 0, draw = 0;

	for (int i = 0; i < n; i++) {
		cin >> a >> b;

		result = judge(a, b);
		if (result == 0)
			draw++;
		else if (result < 0)
			winb++;
		else
			wina++;
	}

	cout << wina << " " << draw << " " << winb << endl;
	cout << winb << " " << draw << " " << wina << endl;

	char MaxA = ((wintimea[C]>wintimea[B]&&wintimea[C]>=wintimea[J])?'C':(wintimea[B]>=wintimea[J]?'B':'J'));
	char MaxB = ((wintimeb[C]>wintimeb[B]&&wintimeb[C]>=wintimeb[J])?'C':(wintimeb[B]>=wintimeb[J]?'B':'J'));
	cout << MaxA << " " << MaxB;
	
	return 0;
}