HDU 2054 A==B

Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
 

Input
each test case contains two numbers A and B.
 

Output
for each case, if A is equal to B, you should print "YES", or print "NO".
 

Sample Input
  
  
1 2 2 2 3 3 4 3
 

Sample Output
  
  
NO YES YES NO
 

很明显这是一道坑爹题……于是我还是被坑了。。

试了几次WA之后,终于AC了,其实很简单,但是需要严谨。

思路:这题由于没有给出条件,因此会出现上千甚至上万位数的测试数据,于是必须用字符串来解题。

然后难点就是去除前置的的0和小数点后不为零数字后面的0,实际上就这两点而已。

用的是C++,代码不长:

#include <iostream>//2054 A==B
#include <string>
using namespace std;

void change(string &str)
{
	if( strchr(str.c_str() , '.' ) )
	{
		int last=str.length();
		while(str[--last]=='0')	str.erase(last,1) ; //删除后置0
		if( str[last]=='.' )   str.erase(last,1) ; //若全为0,删除小数点
	}
	while(str[0]=='0')//前置0的处理
	{
		if( str.length()!=1 ) str.erase(0,1);//若全为0,则保存一个0
		else return;
	}
}
void main()
{
	string a,b;
	while(cin>>a>>b)
	{
		change(a);
		change(b);
		if(a.compare(b)==0) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}

。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值