Ideas: the results "even" those who are correct coins. Then use the array flag [] marked, met again behind the coins are silently ignored.
Processing error coin: we encountered the results of "up" in addition to already determine the correct coins, coin left flag [] -1 +1 meet the right coin flag value results "down" due to differences in the overall coin lighter then left -. 1, the right +1
Result processing: abs greatest value as an error coins.
#include<iostream> #include<string.h> #include<cstdio> #include<cmath> using namespace std; char s1[13],s2[13],con[10]; int flag[12]; int main(){ int n; cin>>n; while(n--){ memset(flag,0,sizeof(flag)); for(int i=1;i<=3;i++){ //scanf_s("%s%s%s",s1,s2,con); cin>>s1>>s2>>con; int len=strlen(s1); if(strcmp(con,"even")==0){ for(int k=0;k<len;k++){ flag[s1[k]-'A']=10; flag[s2[k]-'A']=10; } } else if(strcmp(con,"up")==0){ for(int k=0;k<len;k++){ if(flag[s1[k]-'A']!=10) ++flag[s1[k]-'A']; if(flag[s2[k]-'A']!=10) --flag[s2[k]-'A']; } } else { for(int k=0;k<len;k++){ if(flag[s1[k]-'A']!=10) --flag[s1[k]-'A']; if(flag[s2[k]-'A']!=10) ++flag[s2[k]-'A']; } } } int maxn; maxn=0; int countmax=0; for(int i=0;i<12;i++){ if(flag[i]==10)continue; if(abs(flag[i])>maxn){ maxn=abs(flag[i]); countmax=i; } } if(flag[countmax]>0) printf("%c is the counterfeit coin and it is heavy.\n",'A'+countmax); else printf("%c is the counterfeit coin and it is light.\n",'A'+countmax); } return 0; }