1060 Are They Equal (25分)

23 篇文章 0 订阅
20 篇文章 0 订阅

1 题目

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

2 解析

2.1 题意

给出两个数,问它们写成保留n位小数科学计数法后,是否相等。

2.2 思路

由于科学计数法,只需获取本体部分和指数部分即可:
考虑数据本身,按整数部分是否为0,分为两种情况来讨论:

  • 1 : 0. a 1 a 2 a 3 . . . 0.a_{1}a_{2}a_{3}... 0.a1a2a3...
  • 2 : b 1 b 2.... b m . a 1 a 2 a 3 . . . b_{1}b{2}....b_{m}.a_{1}a_{2}a_{3}... b1b2....bm.a1a2a3...

按有效位数3为来讨论,
对于第一种情况:

由于小数点后面还有0,因此本体部分为小数点后的第一个非零位开始的前3位(即 a k a k + 1 a k + 2 a_{k}a_{k +1}a_{k+2} akak+1ak+2,其中 a k a_{k} ak是小数点后第一个非零位);

指数为小数点与第一个非零位之间0的个数的相反数(如0.001指数为-2)。实现可以,零e初值为0,在小数点后,每出现一个0就让e减1,直到到达最后一位(因为小数点后可能出现全0的情况)或者出现非零位为止。

对于第二种情况:

假设b1为从左到右第一个不为0的数,本体部分为从b1开始的三位;

指数则是小数点前数位的总位数m。

注意:数据可能出现前导0,如000.01或00123.45,为了应对这种情况要删除所有的前导零。

  • 删除所有前导0后,按字符串的第一位是否是小数点,来区分情况一,还是情况二。

  • 解题步骤:

    • 对于情况一的预处理需要:删除前导零、小数点和第一个非零位前面的0;
    • 对于情况二的预处理要:删除前导零,小数点;
    • 预处理后,将剩余位数取有效部分赋值到新字符串,长度不够的有效位后面补0(在前面预处理删除的同时,可以计算指数e)。

注意:如果这个数是0,那么指数也为0;第二种情况可能存在没有小数点的情况。

3 参考代码

#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int N;

string dealNum(string s, int &e){
    int k = 0;//s的下标
    while(s.length() > 0 && s[0] == '0'){//删除前导0
        s.erase(s.begin());
    }

   if(s[0] == '.'){//去掉前导零后是小数点,说明s是小于1的数
       s.erase(s.begin());//删除小数点
       while(s.length() > 0 && s[0] == '0'){//去除第一个非零位前的0
           s.erase(s.begin());
           e--;
       }
   }else{//去掉前导零后不要时小数点,说明s是大于1的数
        while (s.length() > k && s[k] != '.'){//寻找小数点
            k++;
            e++;
        }

        if(k < s.length()){//去除小数点
            s.erase(s.begin() + k);
        }
   }

   if(s.length() == 0){
       e = 0;//去掉前导零后,这个数的长度为0,说明这个数是0
   }

   int num = 0;
   k = 0;
   string res;

   while(num < N){
       if(k < s.length()) res += s[k++];
       else res += '0';
       num++;
   }

    return  res;
}

int main(int argc, char const *argv[]){
    string s1, s2, s3, s4;
    cin >> N >> s1 >> s2;

    int e1 = 0, e2 = 0;
    s3 = dealNum(s1, e1);
    s4 = dealNum(s2, e2);

    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;
}
Here is the function template for isEqualTo: ``` template<typename T> bool isEqualTo(const T& a, const T& b) { return a == b; } ``` Here is an example program that uses isEqualTo with built-in types: ``` #include <iostream> int main() { std::cout << std::boolalpha; std::cout << isEqualTo(5, 5) << std::endl; std::cout << isEqualTo(5, 7) << std::endl; std::cout << isEqualTo('a', 'a') << std::endl; std::cout << isEqualTo('a', 'b') << std::endl; std::cout << isEqualTo(true, true) << std::endl; std::cout << isEqualTo(true, false) << std::endl; std::cout << isEqualTo(3.14, 3.14) << std::endl; std::cout << isEqualTo(3.14, 2.71) << std::endl; return 0; } ``` When we attempt to run this program with the user-defined class type Complex, we get a compilation error because the compiler doesn't know how to compare two Complex objects. To fix this, we can overload the equality operator in the Complex class: ``` class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} double real() const { return m_real; } double imag() const { return m_imag; } private: double m_real; double m_imag; }; bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); } ``` Now when we attempt to run the program with Complex objects, it works as expected: ``` #include <iostream> class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} double real() const { return m_real; } double imag() const { return m_imag; } private: double m_real; double m_imag; }; bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); } template<typename T> bool isEqualTo(const T& a, const T& b) { return a == b; } int main() { std::cout << std::boolalpha; std::cout << isEqualTo(Complex(1.0, 2.0), Complex(1.0, 2.0)) << std::endl; std::cout << isEqualTo(Complex(1.0, 2.0), Complex(3.0, 4.0)) << std::endl; return 0; } ``` Output: ``` true false ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星蓝雨

如果觉得文章不错,可以请喝咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值