PAT刷题——1060 Are They Equal

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

1060 Are They Equal (25 分)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10​5​​ with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:

Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10​100​​, and that its total digit number is less than 100.

Output Specification:

For each test case, print in a line YES if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); or NO if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

题目大意:给定两个数,将他们写成保留N位的科学计数法后是否相等,相等打印YES,并给出转换结果,不等打印NO,给出两个数的转换结果。

  解题思路:将这个数写成科学计数法表示一定会变成这样的形势0.a1a2a3....*10^e因此只需要得出a1a2a3...和e就能够判断大小。然后给出的数有两种情况:

①:0.a1a2a3... 数字本身就是小于1的小数

②:b1b2b3...bm.a1a2a3...数字是大于1的小数

  对于①来说。小数点后面可能跟着若干个0,需要找到小数点后面第一个非零元素,它们之间0的个数就是指数的相反数,即让e初值为0,小数点后面每出现一个0,就让e-1,知道出现第一个非零元素。

  对于②来说。假设b1不为0,那么从开始第一个元素起,第N位这部分就是所求。小数点前的位数就是e的值。让e的初值为0,从前往后枚举,只要不到最后一位或者小数点,就可以让e+1.

  由于数据可能出现前导0(如 000.123 或者 000012.3),所以第一步,先去除前导0,完成之后第一个非零元素是小数点,那么对应①这种情况,否则就是②这种情况.

  进行科学计数法表示时,需要把数字的本体部分,提出前N位来,不足后面用0补齐,最后只要比较本体部分和指数是否相等即可。

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string deal(string s, int &e, int n)
{ //截取前n个有效字符,并计算指数
    int k = 0;
    while (s.length() > 0 && s[0] == '0')
    { //去除前导0
        s.erase(s.begin());
    }
    if (s[0] == '.')
    {                                         //为第一种情况 0.a1a2a3
        s.erase(s.begin());                   //去除小数点
        while (s.length() > 0 && s[0] == '0') //继续去除前导0,记录指数
        {
            s.erase(s.begin());
            e--;
        }
    }
    else
    {                                         //b1b2b3.a1a2a3
        while (k < s.length() && s[k] != '.') //记录小数点前有效数字的个数
        {
            k++;
            e++; //记录指数
        }
        if (k < s.length())
        {                           //说明碰到了小数点
            s.erase(s.begin() + k); //删除小数点
        }
    }
    if (s.length() == 0) //此时浮点数为0
    {
        e = 0;
    }
    int num = 0;
    k = 0;
    string res;
    while (num < n)
    {
        if (k < s.length()) //在字符串范围内,截取相应长度
            res += s[k++];
        else //长度不够用0补齐
            res += '0';
        num++;
    }
    return res;
}
int main()
{
    int n;
    string s1, s2, s3, s4;
    int e1 = 0, e2 = 0; //第一个数的指数和第二个数的指数
    cin >> n;
    cin >> s1 >> s2; //第一个浮点数和第二个浮点数
    s3 = deal(s1, e1, n);
    s4 = deal(s2, e2, n);
    if (s3 == s4 && e1 == e2)
    {
        cout << "YES 0." << s3 << "*10^" << e1 << endl;
    }
    else
    {
        cout << "NO 0." << s3 << "*10^" << e1 << " 0." << s4 << "*10^" << e2 << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/moyefly/article/details/113776630
今日推荐