码迷,mamicode.com
首页 > 其他好文 > 详细

PAT:1018

时间:2020-01-30 21:10:00      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:nts   注释   ack   ensure   oid   重载   命名   ali   highlight   

1018 锤子剪刀布 (20分)

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

技术图片

 

 

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

输入格式:

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

输出格式:

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

这题很讨厌,看着简单,但是要判断很多种情况,这次在写的时候虽然小心了点,但是没有边写边调试各个功能,导致之后多调半个小时,差点以为这题要流掉了

代码部分都给出了注释,因为能力不够,代码长了点,见谅

//甲方命名theone,乙方命名:theshy
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//C:锤子 B:布 J:剪刀
bool compare(char a,char b[])
{
	for (int i = 0; i < 3; i++)
	{
		if (a == b[i])
		{
			return true;
		}
	}
	return false;
}
//求取赢取最多次数的手势所用
int max_three(int C, int B, int J)//fun=0,返回C。fun=1,返回B。fun=2,返回J。
{
	if (C > B&&C>J||C==J&&C>B)
	{
		return 0;
	}
	else if (B > C&& B > J||B==C&&B>J||B==J&&B>C)
	{
		return 1;
	}
	else if (J > C&& J > B)
	{
		return 2;
	}
}
//当甲方出拳头的情况进行判断
void judge_C(char b, int& win_o, int& win_s, int& lose_o, int& lose_s, int& equal_o, int& equal_s,int& count_o_C,int& count_s_B)
{
	if (b == ‘C‘)
	{
		equal_o++;
		equal_s++;
	}
	else if ( b == ‘B‘)
	{
		lose_o++;
		win_s++;
		count_s_B++;
	}
	else if (b == ‘J‘)
	{
		win_o++;
		lose_s++;
		count_o_C++;
	}
}
//当甲方出布的情况
void judge_B( char b, int& win_o, int& win_s, int& lose_o, int& lose_s, int& equal_o, int& equal_s,int& count_o_B,int& count_s_J)
{
	if (b == ‘B‘)
	{
		equal_o++;
		equal_s++;
	}
	else if (b == ‘C‘)
	{
		win_o++;
		lose_s++;
		count_o_B++;
	}
	else if (b == ‘J‘)
	{
		lose_o++;
		win_s++;
		count_s_J++;
	}
}
//当甲方出剪刀的情况
void judge_J( char b, int& win_o, int& win_s, int& lose_o, int& lose_s, int& equal_o, int& equal_s,int& count_o_J,int& count_s_C)
{
	if (b == ‘J‘)
	{
		equal_o++;
		equal_s++;
	}
	else if (b == ‘B‘)
	{
		win_o++;
		lose_s++;
		count_o_J++;
	}
	else if (b == ‘C‘)
	{
		lose_o++;
		win_s++;
		count_s_C++;
	}
}

//获取theone所赢最多的手势
void get_gesture_o(int a,int b,int c,char &gesture_o)//a ,b,c分别代表C,B,J各手势所赢的次数,gesture_o保存所赢手势
{
	if (max_three(a, b, c) == 0) //1表示C:锤子, 0表示B:布, 2表示J:剪刀
	{
		gesture_o = ‘C‘;
	}
	else if (max_three(a, b, c) == 1)
	{
		gesture_o = ‘B‘;
	}
	else if (max_three(a, b, c) == 2)
	{
		gesture_o = ‘J‘;
	}
		
}
//获取theshy所赢最多的手势
void get_gesture_s(int a, int b, int c, char& gesture_s)//a ,b,c分别代表C,B,J各手势所赢的次数,gesture_o保存所赢手势
{
	if (max_three(a, b, c) == 0) //0表示C:锤子, 1表示B:布, 2表示J:剪刀
	{
		gesture_s = ‘C‘;
	}
	else if (max_three(a, b, c) == 1)
	{
		gesture_s = ‘B‘;
	}
	else if (max_three(a, b, c) == 2)
	{
		gesture_s = ‘J‘;
	}

}
int judger(vector<char> &one,vector<char> &shy,  int& win_o, int& win_s, int& lose_o, int& lose_s, int& equal_o, int& equal_s ,char &gesture_o,char &gesture_s,char data[])
{
	int count_o_C = 0, count_o_B = 0, count_o_J = 0;
	int count_s_C = 0, count_s_B = 0, count_s_J = 0;
	for (int i = 0; i < one.size(); i++)
	{
		//判断是否是所要求C,B,J其中一个
		if (!compare(one[i], data) || !compare(shy[i], data))
		{
			return -1;
		}
		//获取赢,平,输的次数
		else
		{
			if (one[i] == ‘C‘)
			{
				judge_C(shy[i],win_o,win_s,lose_o,lose_s,equal_o,equal_s,count_o_C,count_s_B);
			
			}
			else if (one[i] == ‘B‘)
			{
				judge_B(shy[i], win_o, win_s, lose_o, lose_s, equal_o, equal_s,count_o_B,count_s_J);
			
			}
			else if(one[i]==‘J‘)
			{
				judge_J(shy[i], win_o, win_s, lose_o, lose_s, equal_o, equal_s,count_o_J,count_s_C);
			
			}
		}
	}
	//获取手势
	get_gesture_o(count_o_C, count_o_B, count_o_J, gesture_o);
	get_gesture_s(count_s_C, count_s_B, count_s_J, gesture_s);
	/*cout << "count_o_C:" << count_o_C << " " << "count_o_B:" << count_o_B << " " << "count_o_J:" << count_o_J << endl;
	cout << "count_s_C:" << count_s_C << " " << "count_s_B:" << count_s_B << " " << "count_s_J:" << count_s_J << endl;*/
	return 0;
}
int main()
{
	vector<char> theone, theshy;//甲方,乙方
	char a = 0, b = 0;//用作对vector容器的输入中介,vector容器用下标表示的时候不能进行输入操作,这是在标准库中定义,如果你技术够可以自己写一个重载函数
	int N = 0;//比较次数
	cin >> N;
	int win_o = 0, win_s = 0, lose_o = 0, lose_s = 0, equal_o = 0, equal_s = 0;//甲方所赢次数,甲方所输次数,甲方平局次数,乙方所赢次数,乙方所输次数,乙方平局次数
	char gesture_o = 0, gesture_s = 0;//甲方赢得最多的手势,乙方...
	char data[3] = { ‘B‘,‘J‘,‘C‘ };//判断输入是否满足题意所用
	//输入
	for (int i = 0; i < N; i++)
	{
		cin >> a >> b;
		theone.push_back(a);
		theshy.push_back(b);
	}
	int result = 0;
	//函数调用
	result = judger(theone, theshy, win_o, win_s, lose_o, lose_s, equal_o, equal_s, gesture_o, gesture_s, data);//判断输入是否是C,B,J之中
//判断返回值
	if (!result)//观察函数的返回值,返回值为0的时候表示函数正确调用
	{
		//输出
		cout << win_o << " " << equal_o << " " << lose_o << endl;
		cout << win_s << " " << equal_s << " " << lose_s << endl;
		cout << gesture_o << " " << gesture_s;
	}
	else
	{
		return 0;
	}
	return 0;
}

 

PAT:1018

标签:nts   注释   ack   ensure   oid   重载   命名   ali   highlight   

原文地址:https://www.cnblogs.com/zongji/p/12243730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!