DrizzleX

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一道看似很简单但是AC率较低的题(估计是少数人多次重复提交造成的),其实不难,主要是需要考虑的情况比较详细。

http://acm.hdu.edu.cn/showproblem.php?pid=2054

 

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string A;
 9     string B;
10     while(cin>>A>>B)
11     {
12         if((A[0]=='-' && B[0]!='-') || (A[0]!='-' && B[0]=='-'))//符号不同直接输出
13         {
14             cout<<"NO"<<endl;
15         }
16         else
17         {
18             //寻找数字的真正开头
19             string::size_type ArealBegin=A.find_first_not_of("0-.");
20             string::size_type BrealBegin=B.find_first_not_of("0-.");
21             //数字的真正结尾
22             string::size_type ArealEnd;
23             string::size_type BrealEnd;
24 
25             //小数点位置
26             string::size_type Apoint=A.find('.');
27             string::size_type Bpoint=B.find('.');
28             //非0 或小数点的最后一个数字
29             string::size_type AlastNot=A.find_last_not_of("0.");
30             string::size_type BlastNot=B.find_last_not_of("0.");
31 
32 
33             if(find(A.begin(),A.end(),'.')==A.end() && find(B.begin(),B.end(),'.')==B.end())//都没有小数点
34             {
35                 ArealEnd=A.size()-1;
36                 BrealEnd=B.size()-1;
37             }
38 
39             else if(find(A.begin(),A.end(),'.')!=A.end() && find(B.begin(),B.end(),'.')!=B.end())//都有小数点
40             {
41                 if((Apoint-AlastNot)!=(Bpoint-BlastNot))//适用于小数点和最后一个非0数字之间是0
42                 {
43                     cout<<"NO"<<endl;
44                     continue;
45                 }
46 
47                 if((AlastNot<Apoint) && (BlastNot<Bpoint))//非小数
48                 {
49                     ArealEnd=Apoint-1;
50                     BrealEnd=Bpoint-1;
51                 }
52                 else//小数
53                 {
54                     ArealEnd=AlastNot;
55                     BrealEnd=BlastNot;
56                 }
57 
58 
59             }
60             else if(find(A.begin(),A.end(),'.')!=A.end() && find(B.begin(),B.end(),'.')==B.end())//A有B没有
61             {
62                 ArealEnd=Apoint-1;
63                 BrealEnd=B.size()-1;
64             }
65             else if(find(A.begin(),A.end(),'.')==A.end() && find(B.begin(),B.end(),'.')!=B.end())//B有A没有
66             {
67                 ArealEnd=A.size()-1;
68                 BrealEnd=Bpoint-1;
69             }
70             string realA=A.substr(ArealBegin,ArealEnd+1-ArealBegin);
71             string realB=B.substr(BrealBegin,BrealEnd+1-BrealBegin);
72 
73 
74 
75             if(!realA.compare(realB))
76                 cout<<"YES"<<endl;
77             else
78                 cout<<"NO"<<endl;
79 
80         }
81     }
82 }

 

 

另外附上一组测试数据

来源:http://blog.acmj1991.com/?p=880

00000000000000000002222 2222
200 200000
0.00002 0.000020000000000000000000000000000000
00000000000000000000.00002 0.0000200000
0022000 000000000002200
-0022.00200 -000000000000022.002000000000000000000
0.00 0.000000000000000
000000000 0000000000000
000001.00000 1
-0001.000 0001


经过测试题目没有要求
-00001 -1这样的数据,但是该代码可以满足

posted on 2012-04-24 19:29  DrizzleX  阅读(258)  评论(0)    收藏  举报