問題描述:如下圖


程序代碼:/* *問題描述:POJ1013稱硬幣 */ #include <iostream> #include <cstring> using namespace std; char Left[3][7];//天平左邊硬幣 char Right[3][7];//天平右邊硬幣 char result[3][7];//存儲結果 bool IsFake(char c,bool light) {//light為真表示假設假幣為輕,light為假表示假幣為重 for(int i = 0; i<3; ++i)//三次稱量結果,循環3次 { char *pLeft,*PRight;//指向天平兩邊的字符串 if(light) { pLeft = Left[i]; pRight = Right[i]; } else//如果假設假幣是重的,則把稱量結果左右對換 { 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 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<<"Output:"<<endl; cout<<c<<" is the counterfeit coin and it is light./n"; break; } else if (IsFake(c,false)) { cout<<"Output:"<<endl; cout<<c<<" is the counterfeit coin and it is heary./n"; break; } } } return 0; }運行結果:
新聞熱點
疑難解答