HDU 2054 A == B ?(字符串处理 大数比较)题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054
A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 159738 Accepted Submission(s): 26160
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
合理用str.find()、str.substr()函数,可以很简便。
AC代码:
#include<bits/stdc++.h>using namespace std;bool f(string a, string b)
{int a_len = a.length();int b_len = b.length();if (a.find('.') != string::npos) //若存在'.',则进行处理{for (int i = a_len - 1; i>=0; i--){if (a[i] != '0') break;a_len--;}a = a.substr(0, a_len); //从下标为0处截取a_len个字符if (a[a_len - 1] == '.') //最后一个为小数点时,去除{a = a.substr(0, a_len - 1);}}if (b.find('.') != string::npos){for (int i = b_len - 1; i>=0; i--){if (b[i] != '0') break;b_len--;}b = b.substr(0, b_len);if (b[b_len - 1] == '.'){b = b.substr(0, b_len - 1);}}if (a == b) return 1; //直接比较else return 0;
}
int main()
{string a, b;while (cin >> a >> b){if (f(a, b)){cout << "YES" << endl;}else{cout << "NO" << endl;}}return 0;
}