您的位置:首页 > 其它

PAT_Basic 1018

2016-03-08 16:35 155 查看
/*
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式:

输入第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 <string>
#include <vector>
#include <iostream>

using namespace std;

int trans(char a){
if (a == 'C')
return 0;
else if (a == 'J')
return 1;
else if (a == 'B')
return 2;
else
return -1;
}

char trans(int a){
if (a == 0)
return 'C';
else if (a == 1)
return 'J';
else
return 'B';
}

int cmp(char a, char b){
string tmp = {};
tmp.push_back(a);
tmp.push_back(b);

if ((tmp == "CJ") || (tmp
4000
== "JB") || (tmp == "BC"))
return 1;
else if (a == b)
return 0;
else
return -1;
}

char max(int c,int j,int b){
int tmp;
tmp = (c >= j) ? c : j;
tmp = (b >= tmp) ? b : tmp;

return trans(tmp);

}
void main(){
int N;
cin >> N;

char **all;
all = (char**)malloc(sizeof(char*)*N);
all[0] = (char*)malloc(sizeof(char)* N * 2);
for (int i = 1; i < N; i++)
all[i] = all[i - 1] + 2;

int bf[2][3] = { 0 };

int ss[2][3] = { 0 };

for (int i = 0; i < N; i++){
cin >> all[i][0] >> all[i][1];
int ans =cmp(all[i][0], all[i][1]);

switch (ans){
case -1:
bf[0][2]++; bf[1][0]++; ss[1][trans(all[i][1])]++;
break;
case 0:
bf[0][1]++; bf[1][1]++;
break;
case 1:
bf[0][0]++; bf[1][2]++; ss[0][trans(all[i][0])]++;
break;
default:break;
}
}

cout << bf[0][0] << " " << bf[0][1] << " " << bf[0][2] << endl;
cout << bf[1][0] << " " << bf[1][1] << " " << bf[1][2] << endl;

for (int i = 0; i < 2; i++){
cout << max(ss[i][0], ss[i][1], ss[i][2]) << " ";
}
system("pause");
}


A little boring
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: