2692:假币问题
#include <bits/stdc++.h>
using namespace std;
char Left[3][7];
char Right[3][7];
char result[3][5];
bool isFake(char c,bool light){
for(int i=0;i<3;i++){
char *fleft;
char *fright;
if(light==true){
fleft=Left[i];
fright=Right[i];
}
else if(light==false){
fleft=Right[i];
fright=Left[i];
}
char res=result[i][0];
switch(res){
case 'u':
if(strchr(fright,c)==NULL){
return false;
}
break;
case 'e':
if(strchr(fright,c)||strchr(fleft,c)){
return false;
}
break;
case 'd':
if(strchr(fleft,c)==NULL){
return false;
}
break;
}
}
return true;
}
int main(int argc, char** argv) {
int t;
cin>>t;
while(t--){
for(int i=0;i<3;i++){
cin>>Left[i]>>Right[i]>>result[i];
}
for(char c='A';c<='L';c++){
if(isFake(c,true)){
printf("%c is the counterfeit coin and it is light. \n",c);
}
else if(isFake(c,false)){
printf("%c is the counterfeit coin and it is heavy. \n",c);
}
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
char Left[3][7];
char Right[3][7];
char result[3][5];
int w[12];
bool isBalance(){
for(int i=0;i<3;i++){
int leftw=0;
int rightw=0;
for(int j=0;j<6&&Left[i][j]!='\0';j++){
leftw+=w[Left[i][j]-'A'];
rightw+=w[Right[i][j]-'A'];
}
if(result[i][0]=='u'&&leftw>rightw)continue;
else if(result[i][0]=='d'&&leftw<rightw)continue;
else if(result[i][0]=='e'&&leftw==rightw)continue;
else return false;
}
return true;
}
int main(int argc, char** argv) {
int t;
cin>>t;
while(t--){
for(int i=0;i<3;i++){
cin>>Left[i]>>Right[i]>>result[i];
}
memset(w,0,sizeof(w));
int i=0;
for( i=0;i<12;i++){
w[i]=-1;
if(isBalance()){
break;
}
w[i]=1;
if(isBalance()){
break;
}
w[i]=0;
}
printf("%c is the counterfeit coin and it is %s. \n",i+'A',w[i]<0?"light":"heavy");
}
return 0;
}
分治法寻找假币法