[PAT解题报告] 锤子剪刀布 (20)
转载 from http://tech-wonderland.net/blog/pat-basic-level-practice-1016-1020-solutions.html
解题思路: 直接模拟解决, 可能是想考察编程的基本功吧. 按照字典序, B-C-j来做的话, 代码可以更加精简一点. 下面是可以AC的代码:
#include <iostream> #include <algorithm> int comp(char a, char b) { if(a == b) return 0; if( ('C' == a && 'J' == b) || ('J' == a && 'B' == b) || ('B' == a && 'C' == b) ) return 1; return -1; } int mapping(char c) { if('B' == c) return 0; if('C' == c) return 1; return 2; // 'J' } char maxChar(int chArray[3]) { int maxV = *std::max_element(chArray, chArray + 3); if(chArray[0] == maxV) return 'B'; if(chArray[1] == maxV) return 'C'; return 'J'; } int main() { int n; std::cin >> n; char a, b; int iCountWin = 0; int iCoundEven = 0; int cnt1[3] = {0, 0, 0}; int cnt2[3] = {0, 0, 0}; for(int i = 0; i < n; ++i) { std::cin >> a >> b; int ret = comp(a, b); if(1 == ret) { ++iCountWin; ++cnt1[mapping(a)]; } else if(0 == ret) ++iCoundEven; else { ++cnt2[mapping(b)]; } } std::cout << iCountWin << ' ' << iCoundEven << ' ' << n - iCoundEven - iCountWin << std::endl; std::cout << n - iCoundEven - iCountWin << ' ' << iCoundEven << ' ' << iCountWin << std::endl; std::cout << maxChar(cnt1) << ' ' << maxChar(cnt2) << std::endl; return 0; }