標籤:c++ pat
現給出兩人的交鋒記錄,請統計雙方的勝、平、負次數,並且給出雙方分別出什麼手勢的勝算最大。
輸入格式:
輸入第1行給出正整數N(<=105),即雙方交鋒的次數。隨後N行,每行給出一次交鋒的資訊,即甲、乙雙方同時給出的的手勢。C代表“鎚子”、J代表“剪刀”、B代表“布”,第1個字母代表甲方,第2個代表乙方,中間有1個空格。
輸出格式:
輸出第1、2行分別給出甲、乙的勝、平、負次數,數字間以1個空格分隔。第3行給出兩個字母,分別代表甲、乙獲勝次數最多的手勢,中間有1個空格。如果解不唯一,則輸出按字母序最小的解。
#include <stdio.h>#include <stdlib.h>#include <iostream>using namespace std;int change (char a){ switch (a) { case 'B':return 0; case 'C':return 1; case 'J':return 2; } }int main (){ int num; scanf("%d",&num); char hand[3]={'B','C','J'}; char first,second; int k1,k2; int times[3]={0},sort1[3]={0},sort2[3]={0}; for(int i=0;i<num;i++) { cin>>first; cin>>second; k1=change(first); k2=change(second); if( (k1+1)%3==k2) { times[0]++; sort1[k1]++; } else if(k1==k2) { times[1]++; } else if( (k2+1)%3==k1) { times[2]++; sort2[k2]++; } } printf("%d %d %d\n",times[0],times[1],times[2]); printf("%d %d %d\n",times[2],times[1],times[0]); int j1=0,j2=0; for(int i=0;i<3;i++) { if(sort1[i]>sort1[j1]) j1=i; if(sort2[i]>sort2[j2]) j2=i; } printf("%c %c\n",hand[j1],hand[j2]); system("pause"); return 0; }
1018. 鎚子剪刀布