1060. Are They Equal (25)

本文探讨了在计算机中,当浮点数保存到只有3位有效数字时,如何判断两个浮点数是否被视为相等。通过输入指定的位数、两个浮点数,并输出它们是否等价以及标准形式,本文深入分析了简单截断而非四舍五入的处理方式,同时考虑了特殊测试点,如输入0和极大值等。

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.d1...dN*10^k" (d1>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

思路:题目不难,但是要AC很难,要考虑的情况比较多

几个特殊的测试点:

(1)3 0 0.0000000

(2)3 0 0

(3)3 0123 00000123

注意输入0的时候要特别注意 若有效位为3 则输出应该是0.000*10^0 这样的格式,不是0.00*10^0这样

参考AC代码:

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

string standardIt(string s){    //把输入的浮点数转化成标准的格式
    if(s[0]=='.'){
        s = "0"+s;
        return s;
    }
    int index = 1;
    if(s[0]=='0' && s[1]!='.'){
        while(s[index]=='0'){
            if(s[index+1]=='.'){
                break;
            }
            index++;
        }
        if(index==s.size()){
            return "0";
        }
    }else{
        return s;
    }
    s = s.substr(index);
    return s;
}

string int2str(int t){      //把整形转化成字符串整形
    string s="";
    while(t%10!=0){
        char ch = '0'+t%10;
        s = ch+s;
        t /= 10;
    }
    return s;
}

string changeFormat(string s,int n){    //把输入的字符串浮点数转化成要输出的标准格式,再比较
    int temp = 0;       //记录小数点在字符串中的位置
    string result = "";
    if(s=="0"){         //输入为 0 的特殊情况
        result +="0.";
        for(int i=0;i<n;i++){
            result += '0';
        }
        result += "*10^0";
        return result;
    }
    for(int i=0;i<s.size();i++){
        if(s[i]!='.'){
            temp++;
        }else{
            if(temp==1 && s[0]=='0'){   //输入的数小于1
                int indexZero = 0;
                for(int j=2;j<s.size();j++){    //小于0时找到第一个非0数值
                    if(s[j]!='0'){
                        indexZero = j;
                        break;
                    }
                }
                if(indexZero==0){       //输入为0.00000000的情况
                    result +="0.";
                    for(int i=0;i<n;i++){
                    result += '0';
                    }
                    result += "*10^0";
                    return result;
                }
                if(s.size()-indexZero<n){   //非0的个数比有效数个数少 需要补0
                    result += "0.";
                    for(int j=indexZero;j<s.size();j++){
                        result += s[j];
                    }
                    for(int j=0;j<n-s.size()+indexZero;j++){
                        result += '0';
                    }

                    if(indexZero-2==0){
                        result += "*10^0";
                        return result;
                    }else{
                        result += "*10^-";
                        string tmp = int2str(indexZero-2);
                        result += tmp;
                        return result;
                    }

                }else{          //非0个数比有效数多
                    result += "0.";
                    for(int j=indexZero;j<indexZero+n;j++){
                        result += s[j];
                    }
                    if(indexZero-2==0){
                        return result;
                    }else{
                        result += "*10^-";
                        string tmp = int2str(indexZero-2);
                        result += tmp;
                        return result;
                    }
                }
            }else{      //输入佛如值>0
                if(temp>n){
                    result += "0.";
                    for(int j=0;j<n;j++){
                        result += s[j];
                    }
                    result += "*10^";
                    string tmp = int2str(temp);
                    result += tmp;
                    return result;
                }else{
                    int afterPoint = n-temp;
                    result += "0.";
                    for(int j=0;j<temp;j++){
                        result += s[j];
                    }
                    for(int j=temp+1;j<temp+1+afterPoint;j++){
                        result += s[j];
                    }
                    result += "*10^";
                    string tmp = int2str(temp);
                    result += tmp;
                    return result;
                }
            }
        }
    }
    result += "0.";
    if(s.size()>=n){    //输入为整数且位数>有效位数
        for(int i=0;i<n;i++){
            result +=s[i];
        }
        result += "*10^";
        string tmp = int2str(s.size());
        result += tmp;
        return result;
    }else{          //输入为整数且位数<有效位数
        int afterPoint = n-s.size();
        for(int i=0;i<s.size();i++){
            result += s[i];
        }
        for(int i=0;i<afterPoint;i++){
            result += '0';
        }
        result += "*10^";
        string tmp = int2str(n);
        result += tmp;
        return result;
    }
}
int main()
{
    int N;
    string s1,s2;
    cin>>N>>s1>>s2;
    s1 = standardIt(s1);
    s2 = standardIt(s2);
    if(changeFormat(s1,N) == changeFormat(s2,N)){
        cout<<"YES "<<changeFormat(s1,N);
    }else{
        cout<<"NO "<<changeFormat(s1,N)<<" "<<changeFormat(s2,N);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值