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
我认为这道题对于我这样的初学者来说很容易犯用double型变量储存数据的错误,其实不然这道题可能要求输入的数据并不是简单的数像1.111这样的也可能是001.111、001.11100、-001.111 、+001.111这样的数,所以我在这里用到了用string 类型的变量储存数据在对变量进行处理后进行比较。
//代码
#include<iostream>
#include<string>
using namespace std;
string Simp(string a)//实现输入数字的整合,去掉无用的“0”
{
string a1;
int c = 0, d=a.length(), e, f = 0, g = 0;
for (int i = 0; i < a.length(); i++)//判断是否有小数点,并且记住小数点的位置
{
if (a[i] == '.')
{
c = 1;
d = i;
break;
}
}
if (c ==1)
{
if (a[0] == '-' || a[0] == '+')
{
a1 += a[0];
g++;
}
for (int i = g; i<d; i++)
{
if (a[i] != '0')
{
f = 1;
e = i;
}
}
if (f ==1)
{
for (int i = e; i < d; i++)
{
a1 += a[i];
g++;
}
}
if (f == 0)
{
a1 += '0';
g++;
}//除去小数点前的无用“0”
int h = d;
for (int i = d + 1; i < a.length(); i++)
{
if (a[i] != '0')
{
h = i;
}
}
if (h != d)
{
a1 += '.';
g++;
for (int i = d + 1; i <= h; i++)
{
a1+= a[i];
g++;
}
}//除去小数点后的无用“0”
}
if (c == 0)
{
if (a[0] == '-' || a[0] == '+')
{
a1 += a[0];
g++;
}
for (int i = g; i<a.length(); i++)
{
if (a[i] != '0')
{
f = 1;
e = i;
break;
}
}
if (f == 1)
{
for (int i = e; i < d; i++)
{
a1 += a[i];
g++;
}
}
if (f == 0)
{
a1+= '0';
g++;
}
}
return a1;
}
int Judge(string a, string b)//判断两个数字是否相同
{
int e = 1;
if (a[0] != '+' || a[0] != '-')
{
a += a[a.length() - 1];
for (int i = a.length()-2; i >0; i--)
{
a[i] = a[i - 1];
}
a[0]= '+';
}
if (b[0] != '+' || b[0] != '-')
{
b += b[b.length() - 1];
for (int i = b.length(); i >0; i--)
{
b[i] = b[i - 1];
}
b[0] = '+';
}//添加“+”号方便判断
if (a.length() != b.length())
{
e = 0;
}
if (a.length() == b.length())
{
for (int i = 0; i < a.length(); i++)
{
if (a[i] != b[i])
{
e = 0;
break;
}
}
}
return e;
}
int main()
{
string a,b;
while (cin >> a >> b)
{
string a1, b1;
a1=Simp(a);
b1 = Simp(b);
if (Judge(a1, b1) == 1)
{
cout << "YES" << endl;
}
if (Judge(a1, b1) == 0)
{
cout << "NO" << endl;
}
}
}