题解:
思路:将A~L每个字母都枚举一遍(每个字母都有轻和重两种状态),看看是否符合输入数据条件
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 string Left[3], Right[3], Result[3]; 5 6 bool check(char coin, bool weight) // weight = 1表示判断轻,0表示重 7 { 8 string tmp; 9 tmp.push_back(coin); 10 11 for (int i = 0; i < 3; ++i) 12 { 13 string l = Left[i], r = Right[i]; 14 if (!weight) 15 swap(l, r); 16 17 switch (Result[i][0]) 18 { 19 case 'e': 20 if (l.find(tmp) != string::npos || r.find(tmp) != string::npos) 21 return 0; 22 break; 23 case 'u': 24 if (r.find(tmp) == string::npos) 25 return 0; 26 break; 27 case 'd': 28 if (l.find(tmp) == string::npos) 29 return 0; 30 break; 31 default: 32 break; 33 } 34 } 35 return 1; 36 } 37 38 int main() 39 { 40 int t; 41 cin >> t; 42 while (t--) 43 { 44 for (int i = 0; i < 3; ++i) 45 cin >> Left[i] >> Right[i] >> Result[i]; 46 for (char now = 'A'; now <= 'L'; ++now) 47 { 48 if (check(now, 1)) 49 { 50 cout << now << " is the counterfeit coin and it is light. " << endl; 51 break; 52 } 53 if (check(now, 0)) 54 { 55 cout << now << " is the counterfeit coin and it is heavy. " << endl; 56 break; 57 } 58 } 59 } 60 return 0; 61 }
以下是python代码:
1 def check(coin, weight): 2 tmp = coin 3 4 for i in range(3): 5 l = Left[i] if weight == 1 else Right[i] # 类似于c中的三目运算符 6 r = Right[i] if weight == 1 else Left[i] 7 8 if Result[i][0] == 'e': 9 if tmp in l or tmp in r: 10 return False 11 elif Result[i][0] == 'u': 12 if tmp not in r: 13 return False 14 elif Result[i][0] == 'd': 15 if tmp not in l: 16 return False 17 18 return True 19 20 21 t = int(input()) 22 while t > 0: 23 t -= 1 24 Left = [0] * 3 25 Right = [0] * 3 26 Result = [0] * 3 27 28 for i in range(3): 29 Left[i], Right[i], Result[i] = input().split() # 注意这种输入法 30 letter = 'ABCDEFGHIJKL' 31 for now in letter: 32 if check(now, 1): 33 print("{} is the counterfeit coin and it is light. ".format(now)) 34 break 35 elif check(now, 0): 36 print(now + " is the counterfeit coin and it is heavy. ") 37 break
标签:tmp,Right,return,string,问题,Result,假币,now From: https://www.cnblogs.com/nijigasaki14/p/17546199.html