(难)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 Aand 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 (<100)位小数的科学计数法 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); 后,是否相同。若相等,输出YES,并转换结果; 如果不相等,输出NO,并给出两个数的转换结果。

思路:

本题的思路难想而且情况判断非常复杂。

题目要求科学计数法时,两个数是否相等。因此只要判断科学技术法时的本体部分以及指数部分是否相等即可。

对于数据来说,要分为大于 1 与小于 1 来判断,要考虑各种情况下的数字,比如:0000, 000.00, 00123.4, 0.012, 0.00 等

知识点:

string

  1. 添加 #include <string> 以及 using namespace std;
  2. 定义:string str = "abcd";
  3. 下标直接访问 string, str.length() 长度
  4. 读入和输出整个字符串,只能用 cin 和 cout
  5. 迭代器访问:一般采用 3 即可访问, 但 insert() 和 erase() 函数要求以迭代器为参数,定义为:string::iterator it; 得到迭代器 it, 可以采用 *it 来访问 string 里的每一位
  6. erase() 删除单个元素:erase(it),it 为需要删除的元素的迭代器
#include <iostream>
#include <string>
using namespace std;
int n;  //精度
string deal(string s, int& e){      //s为输入的字符串,e 为指数
  int k = 0;
  while(s.length() > 0 && s[0] == '0'){
    s.erase(s.begin());     //取出s的前导零
  }
  if(s[0] == '.'){  //若去掉 0 之后,遇到小数点,则说明s是小于1的小数
    s.erase(s.begin());     //去掉小数点
    while(s.length() > 0 && s[0] == '0'){
      s.erase(s.begin());       //去掉小数点后非零位之前的所有零
      e--;      //每去掉一个零,指数e减一
    }
  }
  else{     //若去掉前导0之后,没有小数点,则找后面的小数点删除
    while(k < s.length() && s[k] != '.'){   //寻找小数点
      k++;
      e++;      //只要不遇到小数点,就让指数++
    }
    if(k < s.length()){   //while结束后,若k < s.length(),说明遇到了小数点
    s.erase(s.begin() + k);     //把小数点删除
    }
  }
  if(s.length() == 0){
    e = 0;          //如果去除前导0之后s的长度变为0,则说明这个数是0
  }
  //此时,需要处理的就是大于1的数字
  int num = 0;
  k = 0;
  string res;       //返回值
  while(num < n){   //只要精度没有到达n
    if(k < s.length())  //只要还有数字,就添加到res末尾
      res += s[k++];
    else
      res += '0';       //否则res末尾添加0
    num++;              //精度加1
  }
  
  return res;
}
int main(){
  string s1, s2, s3, s4;
  cin >> n >> s1 >> s2;
  int e1 = 0, e2 = 0;       //指数
  s3 = deal(s1, e1);
  s4 = deal(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;
}

猜你喜欢

转载自blog.csdn.net/weixin_35093872/article/details/86763678