题意不说了。。。。耐心看看,都看得懂。
思路说下,这个题目提供了一种很好的思路拓展,就是发现问题的隐含条件。这题最主要的隐含条件是如果天平出现了不平衡,那么其余没有放上来的钱币都是真的钱,这个条件是由题目条件仅有一个假币推理得到的。如果不考虑这一点,如果当前是不平衡就会使得没有处理过的钱币被标记为不平衡,而不能通过之前的平衡标记过滤掉这个操作。就会使得结果不正确。。。。。。嗯比如我,wa了一屏。各种标记定义写在代码里面。还有个隐含就是两边个数相等,毕竟如果不等,就是全真也不平衡。
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 333;
char l[N],r[N],w[11];
int hash[N];
//1:重的
//2:没处理过
//0:真的
//-1:轻的
void setEven(int n,bool op)
{//1:置天平上钱币平衡
//0:置天平下钱币平衡
if(op)
for(int i = 0;i < n;i ++)
hash[l[i]] = hash[r[i]] = 0;
else
{
for(int i = 'A';i <= 'L';i ++)
{
bool d = 1;//查找不在天平上的钱币
for(int j = 0;j < n;j ++)
if(l[j] == i || r[j] == i){d = 0;break;}
if(d)hash[i] = 0;
}
}
}
//0:l
//1:r
int jud(bool op,char ch)
{
if(w[0] == 'u'){
if(hash[ch] + (op?-1:1) == 0)
hash[ch] = 0;
if(hash[ch] == 2)
hash[ch] = op?-1:1;
}else if(w[0] == 'd'){
if(hash[ch] + (op?1:-1) == 0)
hash[ch] = 0;
if(hash[ch] == 2)
hash[ch] = op?1:-1;
}
}
void solve()
{
int len = strlen(l);
if(w[0] == 'e')setEven(len,1);
else{
setEven(len,0);
for(int j = 0;j < len;j ++)
jud(0,l[j]);
for(int j = 0;j < len;j ++)
jud(1,r[j]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T --)
{
for(int i = 'A';i <= 'L';i ++)
hash[i] = 2;//标记为处理
for(int i = 0;i < 3;i ++)
{
scanf("%s%s%s",l,r,w);
solve();
}
char ch;//查找假币标记
for(int i = 'A';i <= 'L';i ++)
if(hash[i]!=2 && hash[i]){
ch = i;
break;
}
printf("%c is the counterfeit coin and it is ",ch);
if(hash[ch] == -1)puts("light.");
else puts("heavy.");
}
return 0;
}