标签:情况下 inf alt mic mes char player == 情况
昨天没做,今天补上一题
有点麻烦,一直在想着怎么能够更简洁地处理,先放个初代版本
注意:
char rule[3]={‘B‘,‘C‘,‘J‘}
;输出字母要求按最小的输出,即在遇到次数相同的情况下输出排序较前的字母,因此在比较次数大小的时候,除非 > 前一个数,否则前一个数为max
1 #include<iostream> 2 3 using namespace std; 4 5 int main() { 6 char rule[3] = { ‘B‘,‘C‘,‘J‘ }; 7 /*玩家1的获胜手势计数*/ 8 int player1[3] = { 0 }; //也按BCJ的顺序,计算获胜次数 9 /*玩家2的获胜手势计数*/ 10 int player2[3] = { 0 }; 11 /*以玩家1为基准,计算胜、平、负的局数*/ 12 int judge[3] = { 0 }; 13 /*玩家1、2的获胜次数最多的手势*/ 14 int max1 = 0,int max2 = 0; 15 /*当前双方出拳情况*/ 16 char p1, p2; 17 /*总局数*/ 18 int n = 0; 19 cin >> n; 20 for (int i = 0; i < n; i++) { 21 cin >> p1 >> p2; 22 if (p1 == p2) { 23 judge[1]++; 24 } 25 else if (p1 == ‘C‘ && p2 == ‘J‘) { //p1石头,p2剪刀,p1赢 26 player1[1]++; 27 judge[0]++; 28 } 29 else if (p1 == ‘C‘ && p2 == ‘B‘) { //p1石头,p2布,p2赢 30 player2[0]++; 31 judge[2]++; 32 } 33 else if (p1 == ‘J‘ && p2 == ‘C‘) { //p1剪刀,p2石头,p2赢 34 player2[1]++; 35 judge[2]++; 36 } 37 else if (p1 == ‘J‘ && p2 == ‘B‘) { //p1剪刀,p2布,p1赢 38 player1[2]++; 39 judge[0]++; 40 } 41 else if (p1 == ‘B‘ && p2 == ‘C‘) { //p1布,p2石头,p1赢 42 player1[0]++; 43 judge[0]++; 44 } 45 else if (p1 == ‘B‘ && p2 == ‘J‘) { //p1布,p2剪刀,p2赢 46 player2[2]++; 47 judge[2]++; 48 } 49 } 50 for (int i = 0; i < 2; i++) { //输出玩家1的胜负情况 51 cout << judge[i] << " "; 52 } 53 cout << judge[2] << endl; 54 for (int i = 2; i > 0; i--) { //玩家2的胜负情况即玩家1情况的逆向输出 55 cout << judge[i] << " "; 56 } 57 cout << judge[0] << endl; 58 for (int i = 0; i < 3; i++) { //寻找获胜次数最多的手势 59 if (player1[i] > player1[max1])max1 = i; 60 if (player2[i] > player2[max2])max2 = i; 61 } 62 cout << rule[max1] << " " << rule[max2]; 63 return 0; 64 }
最后一项的检测容易超时,所以推荐用C/C++做本题
标签:情况下 inf alt mic mes char player == 情况
原文地址:https://www.cnblogs.com/SCP-514/p/13237363.html