Poj1013 水题
题目意思就是称量3次,从12个硬币中找出一个假币,可能是轻或者重。只需简单枚举就可以了。
代码:
1 View Code 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 string s[10]; 6 7 bool con(int i,int pos) 8 { 9 int j; 10 string temp=s[i]; 11 for(j=0;j<temp.length();j++) 12 { 13 if(temp[j]-'A'==pos) 14 return true; 15 } 16 return false; 17 } 18 19 bool check(int pos,int w) 20 { 21 int i; 22 string temp; 23 24 25 for(i=3;i<=9;i++) 26 { 27 temp=s[i]; 28 if(temp=="even") 29 { 30 if(con(i-2,pos)||con(i-1,pos)) 31 { 32 return false; 33 } 34 } 35 if(temp=="up") 36 { 37 if(w==1&&!con(i-2,pos)) 38 return false; 39 if(w==0&&!con(i-1,pos)) 40 return false; 41 } 42 if(temp=="down") 43 { 44 if(w==1&&!con(i-1,pos)) 45 return false; 46 if(w==0&&!con(i-2,pos)) 47 return false; 48 } 49 }return true; 50 } 51 52 int main() 53 { 54 int n,i,j,k; 55 cin>>n; 56 for(i=0;i<n;i++) 57 { 58 for(k=1;k<=9;k++) 59 { 60 cin>>s[k]; 61 } 62 63 for(j=0;j<12;j++) 64 { 65 if(check(j,1)) 66 { 67 cout<<(char)('A'+j)<<" is the counterfeit coin and it is heavy."<<endl; 68 break; 69 } 70 if(check(j,0)) 71 { 72 cout<<(char)('A'+j)<<" is the counterfeit coin and it is light."<<endl; 73 break; 74 } 75 } 76 } 77 return 0; 78 }