您的位置:首页 > 其它

HDU 2054

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

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

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
string A;
string B;
while(cin>>A>>B)
{
if((A[0]=='-' && B[0]!='-') || (A[0]!='-' && B[0]=='-'))//符号不同直接输出
{
cout<<"NO"<<endl;
}
else
{
//寻找数字的真正开头
string::size_type ArealBegin=A.find_first_not_of("0-.");
string::size_type BrealBegin=B.find_first_not_of("0-.");
//数字的真正结尾
string::size_type ArealEnd;
string::size_type BrealEnd;

//小数点位置
string::size_type Apoint=A.find('.');
string::size_type Bpoint=B.find('.');
//非0 或小数点的最后一个数字
string::size_type AlastNot=A.find_last_not_of("0.");
string::size_type BlastNot=B.find_last_not_of("0.");

if(find(A.begin(),A.end(),'.')==A.end() && find(B.begin(),B.end(),'.')==B.end())//都没有小数点
{
ArealEnd=A.size()-1;
BrealEnd=B.size()-1;
}

else if(find(A.begin(),A.end(),'.')!=A.end() && find(B.begin(),B.end(),'.')!=B.end())//都有小数点
{
if((Apoint-AlastNot)!=(Bpoint-BlastNot))//适用于小数点和最后一个非0数字之间是0
{
cout<<"NO"<<endl;
continue;
}

if((AlastNot<Apoint) && (BlastNot<Bpoint))//非小数
{
ArealEnd=Apoint-1;
BrealEnd=Bpoint-1;
}
else//小数
{
ArealEnd=AlastNot;
BrealEnd=BlastNot;
}

}
else if(find(A.begin(),A.end(),'.')!=A.end() && find(B.begin(),B.end(),'.')==B.end())//A有B没有
{
ArealEnd=Apoint-1;
BrealEnd=B.size()-1;
}
else if(find(A.begin(),A.end(),'.')==A.end() && find(B.begin(),B.end(),'.')!=B.end())//B有A没有
{
ArealEnd=A.size()-1;
BrealEnd=Bpoint-1;
}
string realA=A.substr(ArealBegin,ArealEnd+1-ArealBegin);
string realB=B.substr(BrealBegin,BrealEnd+1-BrealBegin);

if(!realA.compare(realB))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;

}
}
}


另外附上一组测试数据

来源: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这样的数据,但是该代码可以满足
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: