POJ1013
悲剧了,这道题自己本来会的,小细节处理错误导致WA好几次,最后还是看了下别人的程序,真的无语。
思路很简单:如果天平平衡(even),则两边的coin都是真的。如果不平衡,则针对每一枚coin增加或减少其对应的怀疑值。
最后的目标就是那个怀疑值最大的假币!
C++代码如下:
1 #include<iostream>
2 #include<cstring>
3 #include<string>
4 using namespace std;
5 long abs(long x)
6 {
7 if(x<0)return -x;
8 return x;
9 }
10 long suspect[26],n,len;
11 bool flag;
12 string s1[3],s2[3],s3[3];
13 void work(long len,string w,string l)
14 {
15 for(long j=0;j<len;j++)
16 {
17 if(suspect[w[j]-'A']!=10)
18 suspect[w[j]-'A']++;
19 if(suspect[l[j]-'A']!=10)
20 suspect[l[j]-'A']--;
21 }
22 }
23 int main(void)
24 {
25 cin>>n;
26 long i,j;
27 while(n--)
28 {
29 memset(suspect,0,sizeof(suspect));
30 for(i=0;i<3;i++)
31 {
32 cin>>s1[i]>>s2[i]>>s3[i];
33 len=s1[i].length();
34 if(s3[i].at(0)=='e')
35 {
36 for(j=0;j<len;j++)
37 {
38 suspect[s1[i].at(j)-'A']=10;
39 suspect[s2[i].at(j)-'A']=10;
40 }
41 }
42 else if(s3[i].at(0)=='u')
43 work(len,s1[i],s2[i]);
44 else if(s3[i].at(0)=='d')
45 work(len,s2[i],s1[i]);
46 }
47 long max=0,maxid=0;
48 for(i=0;i<26;i++)
49 {
50 if(suspect[i]!=10&&abs(suspect[i])>max)
51 {
52 max=abs(suspect[i]);
53 maxid=i;
54 }
55 }
56 if(suspect[maxid]>0)
57 cout<<char(maxid+'A')<<" is the counterfeit coin and it is heavy."<<endl;
58 else cout<<char(maxid+'A')<<" is the counterfeit coin and it is light."<<endl;
59 }
60 return 0;
61 }
posted on 2011-10-14 09:00 SilVeRyELF 阅读(366) 评论(0) 收藏 举报
浙公网安备 33010602011771号