PAT-A1060. Are They Equal (25)

来源:互联网 发布:手机知乎怎么发文章 编辑:程序博客网 时间:2024/06/07 15:10

正数部分为0,不为0分开处理。还要处理前导0。

用string,注意erase的应用。

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <map>#include <string> #include <algorithm>using namespace std;int n;string deal(string s, int &e){int k = 0;while (s.length() > 0 && s[0] == '0')s.erase(s.begin());if (s[0] == '.'){s.erase(s.begin());while (s.length() > 0 && s[0] == '0'){s.erase(s.begin());e--;}}else{while (k < s.length() && s[k] != '.'){k++;e++;}if (k < s.length()){s.erase(s.begin()+k);}}if (s.length() == 0)e = 0;k = 0;int num = 0;string res;while (num < n){if (k < s.length())res += s[k++];elseres += '0';num++; }return res;}int main(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);int e1 = 0, e2 = 0;string s1, s2, s3, s4;cin >> n >> s1 >> s2;s3 = deal(s1, e1);s4 = deal(s2, e2);if (s3 == s4 && e1 == e2){printf("YES 0.%s*10^%d", s3.c_str(), e1);}else{printf("NO");printf(" 0.%s*10^%d", s3.c_str(), e1);printf(" 0.%s*10^%d", s4.c_str(), e2);}return 0;}


0 0
原创粉丝点击