有12枚硬幣。其中有11枚真幣和1枚假幣。假幣和真 幣重量不同,但不知道假幣比真幣輕還是重?,F(xiàn)在,用一架天平稱了這些幣三次,告訴你稱的結(jié)果,請(qǐng)你找出假幣并且確定假幣是輕是重(數(shù)據(jù)保證一定能找出來)。
輸入
第一行是測(cè)試數(shù)據(jù)組數(shù)。 每組數(shù)據(jù)有三行,每行表示一次稱量的結(jié)果。銀幣標(biāo)號(hào)為 A-L。每次稱量的結(jié)果用三個(gè)以空格隔開的字符串表示: 天平左邊放置的硬幣 天平右邊放置的硬幣 平衡狀態(tài)。其
中平衡狀態(tài)用up'',down'', 或 ``even''表示, 分別為右 端高、右端低和平衡。天平左右的硬幣數(shù)總是相等的。
輸出
輸出哪一個(gè)標(biāo)號(hào)的銀幣是假幣,并說明它比真幣輕還是重。
輸入樣例
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
輸出樣例
K is the counterfeit coin and it is light.
#include <iostream>
#include <cstring>
using namespace std;
char Left[3][7]; //天平左邊硬幣
char Right[3][7]; //天平右邊硬幣
char result[3][7]; //結(jié)果
bool IsFake(char c,bool light)
//light 為真表示假設(shè)假幣為輕,否則表示假設(shè)假幣為重
{
for(int i = 0;i < 3; ++i) {
char * pLeft,*pRight; //指向天平兩邊的字符串
if(light) {
pLeft = Left[i];
pRight = Right[i];
}
else {//如果假設(shè)假幣是重的,則把稱量結(jié)果左右對(duì)換
pLeft = Right[i];
pRight = Left[i];
}
switch(result[i][0]) { //天平右邊的情況
case 'u':
if ( strchr(pRight,c) == NULL)
return false;
break;
case 'e':
if( strchr(pLeft,c) || strchr(pRight,c))
return false;
break;
case 'd':
if ( strchr(pLeft,c) == NULL)
return false;
break;
}
}
return true;
}
int main(int argc, char const *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)){
cout << c << " is the counterfeit coin and it is light.\n";
break;
}
else if( IsFake(c,false) ){
cout << c << " is the counterfeit coin and it is heavy.\n";
break;
}
}
}
return 0;
}