【PAT】A1060 Are They Equal (string)

【PAT】A1060 Are They Equal (string)

@(PAT)

链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872

思路:
1. 字符串处理,重点是找出来第一个有效数字和小数点的位置。
2. 将它们转换为统一格式科学计数法后,直接比较输出结果。

下面的代码是最初的版本,写得有点复杂,有一个点未过,还没有找出bug

#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int n;

string getRes(string a) {
    bool flag = false;
    bool ifFind = false;
    if (a[0] != '0') flag = true;
    int dotIndexA = -1;
    int firstIndexA = -1;
    //cout << a.length() << endl;
    for (int i = 0; i < a.length(); i++) {
        if (a[i] == '.') {
            dotIndexA = i;
            continue;
        }
        if (a[i] != '0') flag = true;
        if (flag && !ifFind) {
            firstIndexA = i;
            ifFind = true;
        }
    }
    //cout << dotIndexA << " " << firstIndexA << endl;
    if (firstIndexA == -1) {
        string res = "0.";
        for (int i = 0; i < n; i++) {
            res += "0";
        }
        res += "*10^0";
        return res;
    }
    int e = 0;
    if (dotIndexA == -1) {
        //cout << "equal -1" << endl;
        e = a.length()- firstIndexA;
    }
    else {
        if (dotIndexA > firstIndexA) {
            e = dotIndexA- firstIndexA;
        }
        else {
            e = dotIndexA - firstIndexA+ 1;
        }
    }
    //cout << e << endl;
    string res = "0.";
    int i = 0;
    int nums = 0;
    while (nums < n) {
        if (i + firstIndexA < a.length()) {
            if (a[i + firstIndexA]<= '9'&& a[i + firstIndexA]>= '0') {
                res += a[i + firstIndexA];
                nums++;
            }
        }
        else {
            res += '0';
            nums++;
        }
        i++;
    }
    res += "*10^";
    char num = abs(e) + '0';
    if (e < 0) res += '-';
    res += num;
    return res;
}

int main()
{
    string a, b;
    cin >> n;
    cin >> a;
    cin >> b;
    string resA= getRes(a);
    string resB= getRes(b);
    if (resA == resB) {
        cout << "YES "<< resA;
    }
    else {
        cout << "NO " << resA << " " << resB;
    }
}

后来参考了资料https://blog.csdn.net/mapoos/article/details/79354693,能够AC版本:
重点是对前导0的处理

#include <iostream>
#include <string>
using namespace std;

string solve(string s, int &exp, int acc)
{
    string ans = "0.";
    int i = 0;
    exp = 0;

    while(s[0] == '0')          // 先去'0'
        s.erase(0, 1);

    if (s[0] == '.')            // 第一次去'0'后若直接为小数点,则该数小于1
    {
        s.erase(0, 1);          
        while (s[0] == '0')     // 去除小数点后的'0',每去一个'0'指数减1
        {
            s.erase(0, 1);
            exp--;
        }
    }
    else        // 该数大于1时
    {
        while (s[i] != '.' && i < s.length())       // 寻找小数点
        {
            exp++;
            i++;
        }
        if (s[i] == '.')        // 若存在小数点则去掉
            s.erase(i, 1);
    }
    if (s.length() == 0)        // 若字符串剩余长度为0,说明该数为0,指数置为0
        exp = 0;    

    i = 0;
    while (i < acc)             // 当精度不足时
    {
        if (i < s.length())     // 若剩余字符串中还有数字
            ans += s[i++];
        else                    // 数字不够末位补'0'
        {
            ans +='0';
            i++;
        }
    }
    ans = ans + "*10^";

    return ans;
}

int main()
{
    int n;
    string a, b;
    string x, y;
    int xe, ye;         // 存储指数

    cin >> n >> a >> b;
    x = solve(a, xe, n);
    y = solve(b, ye, n);

    if (x == y && xe == ye)     // 既要小数部分相等,也要指数相等
        cout << "YES " << x << xe;
    else
        cout << "NO " << x << xe << " " << y << ye; 
}

猜你喜欢

转载自blog.csdn.net/timso1997/article/details/80682271
今日推荐