【PAT甲级】 1060 Are They Equal 测试点6

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×105 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 10100, 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

本来看了网上一大堆关于测试点6的说法,也看了柳神的代码,该注意的地方我都注意了,对于0,00,0.00,00.00我的输出都是没问题的,实在是找不到测试点6我过不了的原因……(手动崩溃)

后来朋友看了我的代码,在她那边编译器运行0,00,0.00,00.00的输出和我这边是不一样的,无缘无故多了空格,后来我又拿pta自带的编译器运行0,00,0.00,00.00样例,发现和我本地vscode和朋友那边的devc++的输出结果都不一样,一个字符串只输出了一半。

我再仔细看了我的代码,发现

int i = indexA;
    while (answerA.size() < n + 2)
    {
        if (strA[i] != '.')
            answerA += strA[i++];
        else
            i++;
    }

indexA等于strA.size()时,strA[i]是越界的,我就奇怪为什么vscode里面越界了也不报错,竟然输出的还是正确答案,后来才发现str[i]的输出在不同编译器是不一样的,在vscode里面是空字符,devc++里面是空格字符,在pat里面是停止字符(感觉有点类似于c里面char的“\0”,输出字符串的话遇“\0”就停止)算是误打误撞在vscode里面输出正确,代码本身就是有问题的。解决越界问题就AC了。

AC的代码给到大家

#include <iostream>
using namespace std;
int main()
{
    int n, posA, posB;
    string strA, strB;
    cin >> n >> strA >> strB;
    string strACopy = strA, strBCopy = strB;
    strA += '.';
    strB += '.';
    posA = strA.find(".");
    posB = strB.find(".");
    strA.insert(strA.size(), n, '0');
    strB.insert(strB.size(), n, '0');
    //cout<<strA<<" "<<strB<<endl;
    int indexA = 0;
    for (indexA = 0; indexA < strA.size(); indexA++)
        if (strA[indexA] != '0' && strA[indexA] != '.')
        {
            if (posA < indexA)
                posA++;
            posA = posA - indexA;
            break;
        }
    int indexB = 0;
    for (indexB = 0; indexB < strB.size(); indexB++)
        if (strB[indexB] != '0' && strB[indexB] != '.')
        {
            if (posB < indexB)
                posB++;
            posB = posB - indexB;
            break;
        }
    string answerA = "0.";
    string answerB = "0.";
    int i = indexA;
    while (answerA.size() < n + 2)
    {
        if (strA[i] != '.')
            answerA += strA[i++];
        else
            i++;
    }
    i = indexB;
    while (answerB.size() < n + 2)
    {
        if (strB[i] != '.')
            answerB += strB[i++];
        else
            i++;
    }
    //cout<<indexA<<" "<<indexB<<endl;
    //cout<<strA.size()<<strB.size()<<endl;
    if (indexA == strA.size())
    {
        answerA="0.";
        answerA.insert(answerA.size(), n, '0');
        //cout<<answerA<<endl;
        posA = 0;
    }
    if (indexB == strB.size())
    {
        answerB="0.";
        answerB.insert(answerB.size(), n, '0');
        //cout<<answerB<<endl;
        posB = 0;
    }
    answerA = answerA + "*10^" + to_string(posA);
    answerB = answerB + "*10^" + to_string(posB);
    if (answerA == answerB)
        cout << "YES " << answerA << endl;
    else
        cout << "NO " << answerA << " " << answerB << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值