Counterfeit Dollar (枚举)
~题目连接~
http://poj.org/problem?id=1013
输入
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
结果
K is the counterfeit coin and it is light.
even 都为真,标记(为保持好看,标记写成了累加,最后判断是否为0)
up 右重左轻,分类累加
down 右轻左重,分类累加
k 记录假币出现次数
最后判断未被标记,且与假币出现次数相等的数据(若出现多枚假币,只能说明输入数据错误……^_^……)
#include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 30 struct node { int up,down,even; } Q[maxn]; int main() { int n; char L[maxn],R[maxn],S[maxn]; while(~scanf("%d",&n) && n>0) { while(n--) { int i,k=0; memset(Q,0,sizeof(Q)); for(int l=0; l<3; l++) { scanf("%s%s%s",L,R,S); int x=strlen(L); int y=strlen(R); if(!strcmp(S,"even"))//都为真 { for(i=0; i<x; i++) Q[L[i]-'A'].even++; for(i=0; i<y; i++) Q[R[i]-'A'].even++; } if(!strcmp(S,"up"))//右重左轻 { k++;//计数,用来判断在那一数组(轻 还是 重) for(i=0; i<x; i++) Q[L[i]-'A'].up++; for(i=0; i<y; i++) Q[R[i]-'A'].down++; } if(!strcmp(S,"down"))//右轻左重 { k++; for(i=0; i<x; i++) Q[L[i]-'A'].down++; for(i=0; i<y; i++) Q[R[i]-'A'].up++; } } for(int j=0; j<12; j++) { if(!Q[j].even && Q[j].down==k)//轻假币 { printf("%c is the counterfeit coin and it is light.\n",j+'A'); break; } else if(!Q[j].even && Q[j].up==k)//重假币 { printf("%c is the counterfeit coin and it is heavy.\n",j+'A'); break; } } } } return 0; }
附带测试数据(注意:这组数据过了后在提交)
sample input 12 ABCD EFGH even ABCI EFJK up ABIJ EFGH even AGHL BDEC even JKI ADE up J K even ABCDEF GHIJKL up ABC DEF even I J down ABCDEF GHIJKL up ABHLEF GDIJKC down CD HA even A B up B A down A C even A B up B C even DEFG HIJL even ABC DEJ down ACH IEF down AHK IDJ down ABCD EFGH even AB IJ even A L down EFA BGH down EFC GHD even BA EF down A B up A C up L K even ACEGIK BDFHJL up ACEGIL BDFHJK down ACEGLK BDFHJI down ACEGIK BDFHJL up ACEGIL BDFHJK down ACEGLK BDFHJI up sample output K is the counterfeit coin and it is light. I is the counterfeit coin and it is heavy. I is the counterfeit coin and it is light. L is the counterfeit coin and it is light. B is the counterfeit coin and it is light. A is the counterfeit coin and it is heavy. A is the counterfeit coin and it is light. L is the counterfeit coin and it is heavy. A is the counterfeit coin and it is light. A is the counterfeit coin and it is heavy. L is the counterfeit coin and it is light. K is the counterfeit coin and it is heavy.
……*_*……