HDU-2054 A == B ?

题目:

A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 89709 Accepted Submission(s): 14213


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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// oj.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
#include <string>

using namespace std;

void change(string &str)
{
if (str.find('.') != string::npos)
{
int last = str.size();
while (str[--last] == '0')
str.erase(last, 1);
if (str[last] == '.')
str.erase(last, 1);
}
while (str[0] == '0')
{
if (str.length() != 1)
str.erase(0, 1);
else
return;
}
}
int main()
{
string a, b;
while (cin >> a >> b)
{
change(a);
change(b);
if (a == b) cout << "YES" << endl;
else cout << "NO" << endl;
}
}

解析&吐槽:

看起来好像很简单,其实完全不是。首先, 题目没说数字可以用 64 位整形表示,其次,数字也可能带有多余的 0。

比如:

  • 00001 和 1
  • 1.0100 和 1.01

所以我把数字作为字符串读入,然后用一个函数删除多余的 0。处理之后的数字就可以方便 地比较了。需要注意的是如果小数点后面的的数字都被删去的话,小数点也需要被删掉。

本作品采用 署名-相同方式共享 4.0 国际 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 “不科学的科学君” (Liu233w) 与博客链接: https://liu233w.github.io ,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

加载评论框需要翻墙