PAT-B 1018. 锤子剪刀布

1. 依题意对每一局(每一行输入)进行比较、统计

2. 统计结果可以记录在一张map里,或者索性用a_cnt[100], b_cnt[100]这样的数组统计(B,C,J的ASCII值都小于100)


代码:

#include <iostream>
#include <cstring>

using namespace std;

inline bool cmp(char a, char b)
{
	return (a=='C'&&b=='J') || (a=='J'&&b=='B') || (a=='B'&&b=='C');
}

int main()
{
	int n;
	int a_cnt[100]={0, 0}, b_cnt[100]={0, 0};
	int a_record[3]={0, 0}, b_record[3]={0, 0};
	char a, b;
	cin >> n;

	for (int i = 0; i < n; ++ i)
	{
		cin >> a >> b;
		if (a == b)
		{
			++ a_record[1];
			++ b_record[1];
		} else if (cmp(a, b)) // a win
		{
			++ a_record[0];
			++ b_record[2];
			++ a_cnt[a];
		} else				  // b win
		{
			++ a_record[2];
			++ b_record[0];
			++ b_cnt[b];
		}
	}

	cout << a_record[0] << " " << a_record[1] << " " << a_record[2] << endl;
	cout << b_record[0] << " " << b_record[1] << " " << b_record[2] << endl;

	// BCJ -> CJB
	int maxx = a_cnt['B'];
	char maxx_choice = 'B'; 
	if (a_cnt['C'] > maxx)
	{
		maxx = a_cnt['C'];
		maxx_choice = 'C';
	}
	if (a_cnt['J'] > maxx)
	{
		maxx = a_cnt['J'];
		maxx_choice ='J';
	}
	cout << maxx_choice << " ";

	maxx = b_cnt['B'];
	maxx_choice = 'B';
	if (b_cnt['C'] > maxx)
	{
		maxx = b_cnt['C'];
		maxx_choice = 'C';
	}
	if (b_cnt['J'] > maxx)
	{
		maxx = b_cnt['J'];
		maxx_choice = 'J';
	}
	cout << maxx_choice;

	return 0;
}


你可能感兴趣的:(C++,PAT-B)