PAT 乙级 1018 锤子剪刀布
1. 题目描述及在线测试位置
1.1 根据规则:锤子胜剪刀、布胜锤子、剪刀胜布,统计得到 获胜次数、平手次数、失败次数,并打印出什么手势下获胜机率最高(如果解不唯一,则输出按字母序最小的解:B 布 > C 锤子 > J 剪刀)
1.2 在线测试位置:PAT 1018 锤子剪刀布
2. 具体实现
2.1 基本思路
根据规则设定条件分支语句,统计相关数据
2.2 AC代码
#include <iostream>
using namespace std;
void Print(int index);
int max(int a[], int n);
int main()
{
int n,success=0,tie=0,fail=0; //输入的n条数据、胜次数、平次数、败次数
char gesture_A, gesture_B; //甲的手势、乙的手势
int A[3] = {0,0,0}, B[3] = { 0,0,0 }; //分别对应出布、石头、剪刀的获胜次数 B C J
int index; //index用于指示出什么手势获胜机率最高:0 B, 1 C, 2 J
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> gesture_A >> gesture_B;
if (gesture_A == gesture_B)
tie++;
else if (gesture_A=='C' && gesture_B=='J')
{
success++;
A[1]++;
}
else if (gesture_A == 'C' && gesture_B=='B')
{
fail++;
B[0]++;
}
else if (gesture_A == 'B' && gesture_B=='J')
{
fail++;
B[2]++;
}
else if (gesture_A == 'B' && gesture_B=='C')
{
success++;
A[0]++;
}
else if (gesture_A == 'J' && gesture_B == 'C')
{
fail++;
B[1]++;
}
else if (gesture_A == 'J' && gesture_B == 'B')
{
success++;
A[2]++;
}
}
cout << success<<" " << tie<<" " << fail << endl;
cout << fail << " " << tie << " " << success << endl;
index = max(A, 3);
Print(index);
cout << " ";
index = max(B, 3);
Print(index);
return 0;
}
int max(int a[], int n)
{
int index = 0;
for (int i = 1; i < n; i++)
if (a[index] < a[i])
index = i;
return index;
}
void Print(int index)
{
if (index == 0)
cout << "B";
else if (index == 1)
cout << "C";
else
cout << "J";
}