http://poj.org/problem?id=1013 (题目链接)
题意
12个硬币中有1个是假的,给出3次称重结果,判断哪个硬币是假币,并且判断假币是比真币中还是比真币轻。
Solution
很久以前写的题了,现在翻了翻发现思路还是不错的。
http://blog.csdn.net/lyy289065406/article/details/6661421
细节
像这种比较水的与字符串相关的题目用string做一些处理会方便很多,然而这道题好像都差不多。
代码
// poj1013
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#define MOD 1000000007
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[12];
string s1,s2,s3;
int main() {
int T;
scanf("%d",&T);
while (T--) {
int cnt=0;
for (int i=0;i<12;i++) a[i]=0;
for (int o=1;o<=3;o++) {
cin>>s1>>s2>>s3;
if (s3=="up") {
for (int j=0;j<(int)s1.size();j++) a[s1[j]-'A']++;
for (int j=0;j<(int)s2.size();j++) a[s2[j]-'A']--;
cnt++;
}
if (s3=="down") {
for (int j=0;j<(int)s1.size();j++) a[s1[j]-'A']--;
for (int j=0;j<(int)s2.size();j++) a[s2[j]-'A']++;
cnt++;
}
if (s3=="even")
for (int j=0;j<(int)s1.size();j++) a[s1[j]-'A']=a[s2[j]-'A']=6;
}
for (int i=0;i<12;i++) {
if (a[i]==cnt) printf("%c is the counterfeit coin and it is heavy.
",i+'A');
if (a[i]==-cnt) printf("%c is the counterfeit coin and it is light.
",i+'A');
}
}
return 0;
}