每次做这个题都会wa……TAT
题意:一共12枚硬币,有1个是假的,假的硬币重量跟别的不一样,用一杆天平量三次,给出每次量的方案和结果,问哪个是假币,是重还是轻。
解法:如果天平两端相等说明这两端的硬币都是真币,用一个数组记录真币,如果天平不平衡,用一个新数组初始化是0,重的硬币+1,轻的硬币-1,最后统计一下哪个硬币不确定是真币而且重量的绝对值最大。
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #define LL long long using namespace std; int main() { int T; while(~scanf("%d", &T)) { while(T--) { int num[300]; int flag[300] = {0}; for(int i = ‘A‘; i <= ‘L‘; i++) num[i] = 0; string s1, s2, s3; for(int j = 0; j < 3; j++) { cin >> s1 >> s2 >> s3; if(s3 == "even") { for(int i = 0; i < s1.size(); i++) { flag[s1[i]] = 1; flag[s2[i]] = 1; } /*for(int i = ‘A‘; i <= ‘L‘; i++) cout << num[i] << " "; puts("");*/ } else if(s3 == "up") { for(int i = 0; i < s1.size(); i++) { num[s1[i]]++; num[s2[i]]--; } } else { for(int i = 0; i < s1.size(); i++) { num[s1[i]]--; num[s2[i]]++; } } } int ans = 0, pos = 0; for(int i = ‘A‘; i <= ‘L‘; i++) { if(flag[i] == 1) continue; else { if(abs(num[i]) > ans) { ans = abs(num[i]); pos = i; } } } cout << (char)pos << " is the counterfeit coin and it is "; if(num[pos] < 0) puts("light."); else puts("heavy."); } } return 0; }
时间: 2024-10-05 17:01:13