题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860
My思路:
先用两个字符串储存这两个实数,然后再用另外两个字符串储存去掉符号和前后多余的0后的新"实数",最后只需要比较两个化简后的新字符就ok了。
My代码实现:
1 #include<iostream> 2 using namespace std; 3 string simplify(string s) { //去字符串s的正负号和首尾多余的0 4 string a; 5 int k = s.size()-1, l = 0, t = 0; 6 for(int i = 0; i < s.size(); i++) //判断是否是小数,如果是小数就需要去除尾部多余的0 7 if(s[i] == '.') { 8 t = 1; 9 break; 10 } 11 if(s[0] == '+' || s[0] == '-') l++; //去除符号 12 while(s[l] == '0') l++; //去除前面多余的0 13 //cout << l << endl; 14 if(t) { //去除后面多余的0 15 while(s[k] == '0') k--; 16 } 17 if(s[k] == '.') k--; 18 //cout << k << endl; 19 while(l <= k) a += s[l++]; 20 return a; 21 } 22 int main() { 23 //string x = "-002540000.0000025000000"; 24 //cout << simplify(x); 25 string A, B, a, b; 26 char fa, fb; 27 while(cin >> A >> B) { 28 a = simplify(A); //去除符号和首尾多余的0 29 b = simplify(B); 30 //cout << a << " " << b << endl; 31 if(A[0] != '+' && A[0] != '-') fa = '+'; //正负号拿出来单独判断 32 else fa = A[0]; 33 if(B[0] != '+' && B[0] != '-') fb = '+'; 34 else fb = B[0]; 35 if(((fa == '+' && fb == '-') || (fa == '-' && fb == '+')) && (a != "" && b != "")) cout << "NO "; //这里千万要考虑+0=-0的情况 36 else { 37 int i, p = 1; 38 for(i = 0; i < a.size() && i < b.size(); i++) 39 if(a[i] != b[i]) { 40 cout << "NO "; 41 p = 0; 42 break; 43 } 44 if(i == b.size() && i == a.size()) cout << "YES "; 45 else if(p) cout << "NO "; 46 } 47 } 48 }
开始写于:2016.7.31 ----志银