PAT甲级刷题记录——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×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 YESif 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 NOif 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

思路

前排提示:这题的坑点非常非常非常多!!不要看《算法笔记》上晴神就这么轻描淡写把这题写好了,自己写起来真的非常烦!!

首先过样例是很简单的一件事,给出的数无非就是①0.xxx;②xxx.xxx这两种情况,下面我来分情况讨论:

①如果是0.xxx这种情况的话,那么就要计算小数点和第一个数字之间有多少个0,这样就能知道我们的指数大小了(比如0.00123,如果N是3,那么按照题目意思写就是0.123*10^-2),但这里还有两种情况:I.如果说有效数字比给定的N要小,那么显然,需要补0,一直补到有效数字总共为N位;II.如果说有效数字比给定的N要大或者相等,那么直接截取N位长度即可。

②如果是xxx.xxx这种情况的话,那么又能分两种情况出来:I.如果说整数部分的位数已经大于等于N了,那么很好,直接截取N位子串,然后指数就是整数部分的位数(比如12345.67,如果N为3,那么就是0.123*10^5);II.如果说整数部分的位数小于N,那么指数还是一样的,是整数部分的位数,但是呢,d1~dN这部分该怎么取呢,也很简单,只要把小数点删了,然后再截取N位子串(比如12.345,如果N为3,那么就是0.123*10^2)。

但是!!只考虑这四种情况就能AC了吗?这题目可没这么简单(说实话,写完这四种情况也挺麻烦的),如果只考虑这四种情况提交的话,分数应该是21分,然后测试点4和6过不了(不要问我为什么这么清楚……)。
在这里插入图片描述
然后测试点4的话应该是输入有前导0的情况(我一开始不信邪,不听晴神说的要删除前导0,因为题目说了给出的A和B是float number,那怎么会出现诸如00012这种情况呢……事实上还真会),然后就对输入的A和B进行判断,删除前导0,但是删的时候要注意,千万别把正常数据的0也删了(比如000.001023,删完应该是0.001023),当你好不容易处理完A、B前导0的问题之后,恭喜你,拿到了23分(呵呵了……)。

那么还有一个测试点6是什么情况呢,就是当输入的数据是0的时候,这个其实题目已经给了一丢丢提示:

d[1]>0 unless the number is 0

也就是说题目是可能出现数据是0的情况的,那么这个时候就要判断一下了:

  • 如果是单纯的整数0,那么我们的代码其实是不用做任何变化的,因为写的指数部分判断就是整数的位数(删除前导0的时候其实已经把0删掉了,因此此时字符串B的长度应该是0,所以指数也是0);
  • 如果是0.000…(若干个0)的情况的话,那么只要判断一下,如果用来作为有效数字部分的字符串tmp在循环结束之后长度依然为0,就说明是0.000…(若干个0)这种情况,那么只要重置一下指数部分就行了(因为之前指数部分的是按照0的个数减下去的,小数点后面有几个0,指数部分就是负几,但是这种情况显然是10^0,因此,需要重置一下,即:zhishuA = 0(或者zhishuB = 0))。

【另外】我的删除前导0部分的代码其实还是有错误的,没有考虑到001230.456这种情况,如果是这种情况的话,我的代码会直接删成0.456……但是AC了,懒得改了……

最后提醒一下自己:此类题目没有固定套路,分多少种情况最好写在纸上,然后用代码一个一个模块去实现,至少要先过样例,不要想着一遍下来能直接AC,这样考虑得太多只会越想越烦,最后就会导致花了大量时间结果连样例都过不了(一定要先确保过样例!!然后再在这个基础上修修补补)。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
int main(){
    int N;
    string A, B, xiaoshuA, xiaoshuB;
    cin>>N>>A>>B;
    int zhishuA = 0, zhishuB = 0;
    if(A.find(".")!=string::npos){//如果存在小数点
        int pos = A.find(".");
        if(A[pos-1]=='0'){//如果小数点前一位依然是0,说明是0000.xxxx的情况
            A = A.substr(pos-1);//从pos-1一直到末尾的子串
        }
        else{//如果小数点的前一位不是0,说明是00xx.xxxx的情况
            while(A[0]=='0'){//碰到第一个不为0的数,直接break,防止删除正常部分的0,比如001203.123这种情况
                A.erase(A.begin());//删0
            }
        }
    }
    else{//如果不存在小数点
        while(A[0]=='0'){//碰到第一个不为0的数,直接break,防止删除正常部分的0,比如001203.123这种情况
            A.erase(A.begin());//删0
        }
    }
    if(B.find(".")!=string::npos){//如果存在小数点
        int pos = B.find(".");
        if(B[pos-1]=='0'){//如果小数点前一位依然是0,说明是0000.xxxx的情况
            B = B.substr(pos-1);//从pos-1一直到末尾的子串
        }
        else{//如果小数点的前一位不是0,说明是00xx.xxxx的情况
            while(B[0]=='0'){//碰到第一个不为0的数,直接break,防止删除正常部分的0,比如001203.123这种情况
                B.erase(B.begin());//删0
            }
        }
    }
    else{//如果不存在小数点
        while(B[0]=='0'){//碰到第一个不为0的数,直接break,防止删除正常部分的0,比如001203.123这种情况
            B.erase(B.begin());//删0
        }
    }
    if(A[0]=='0'&&A[1]=='.'){//0.xxx的小数
        string tmp;
        int i = 2;
        while(A[i]=='0'){
            zhishuA--;
            i++;
        }
        while(i<A.length()){
            tmp += A[i];
            i++;
        }
        if(tmp.length()==0){//如果此时tmp没有任何东西,说明是0.000...这种类型,也就是0的情况
            zhishuA = 0;
        }
        if(tmp.length()<N){//如果小数点后部分的位数比题目要求的N要少,说明要补0
            while(tmp.length()<N){
                tmp += '0';//补0
            }
        }
        else if(tmp.length()>N){//如果小数点后部分的位数比N要多,说明要舍去
            tmp = tmp.substr(0, N);
        }
        xiaoshuA = tmp;//最终得到了A的小数部分
    }
    else{//xxx.xxx的整数
        int lenZhengshu = 0;
        if(A.find(".")!=string::npos) lenZhengshu = A.find(".");
        else lenZhengshu = A.length();
        if(lenZhengshu>=N){//如果整数部分的长度已经满足题目N的要求了
            xiaoshuA = A.substr(0, N);
            zhishuA = lenZhengshu;
        }
        else{
            if(A.find(".")!=string::npos){
                A.erase(A.begin()+lenZhengshu);//删除小数点
            }
            else{//如果没有小数点,而且整数部分比N要小,说明要补0
                while(A.length()<N){
                    A += '0';
                }
            }
            xiaoshuA = A.substr(0, N);
            zhishuA = lenZhengshu;
        }
    }
    if(B[0]=='0'&&B[1]=='.'){//0.xxx的小数
        string tmp;
        int i = 2;
        while(B[i]=='0'){
            zhishuB--;
            i++;
        }
        while(i<B.length()){
            tmp += B[i];
            i++;
        }
        if(tmp.length()==0){//如果此时tmp没有任何东西,说明是0.000...这种类型,也就是0的情况
            zhishuB = 0;
        }
        if(tmp.length()<N){//如果小数点后部分的位数比题目要求的N要少,说明要补0
            while(tmp.length()<N){
                tmp += '0';//补0
            }
        }
        else if(tmp.length()>N){//如果小数点后部分的位数比N要多,说明要舍去
            tmp = tmp.substr(0, N);
        }
        xiaoshuB = tmp;//最终得到了B的小数部分
    }
    else{//xxx.xxx的整数
        int lenZhengshu = 0;
        if(B.find(".")!=string::npos) lenZhengshu = B.find(".");
        else lenZhengshu = B.length();
        if(lenZhengshu>=N){//如果整数部分的长度已经满足题目N的要求了
            xiaoshuB = B.substr(0, N);
            zhishuB = lenZhengshu;
        }
        else{
            if(B.find(".")!=string::npos){
                B.erase(B.begin()+lenZhengshu);//删除小数点
            }
            else{//如果没有小数点,而且整数部分比N要小,说明要补0
                while(B.length()<N){
                    B += '0';
                }
            }
            xiaoshuB = B.substr(0, N);
            zhishuB = lenZhengshu;
        }
    }
    if(xiaoshuA==xiaoshuB&&zhishuA==zhishuB) cout<<"YES 0."<<xiaoshuA<<"*10^"<<zhishuA;
    else cout<<"NO 0."<<xiaoshuA<<"*10^"<<zhishuA<<" 0."<<xiaoshuB<<"*10^"<<zhishuB;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值