1060 Are They Equal (25 分)字符串转换浮点型数值

版权声明:假装这里有个版权声明…… https://blog.csdn.net/CV_Jason/article/details/85304491

题目

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 × 1 0 5 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 1 0 100 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 ] 1 0 k 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.12010^3 0.12810^3

解题思路

  题目大意: 给你两个数,以及一个正整数N,把这两个数都转换成保留位数为N的科学计数法,比较保留位数之后的数据,如果相等,输出“YES”以及对应的数,如果不相等,输出“NO”以及两者的转换结果。
  解题思路: 这道题的难点在如何进行转换,从几个一般性的例子,我们总结规律。一般而言,有三种情况,一种是有小数点但是数比较大的,一种是小数点后的非常小的数,一种是没有小数点的整数(可以看做第一种情况的特例)。

  在这里插入图片描述
  1. 第一步先确定小数点的位置,如果没有小数点,那么显然是一个整数,其数值长度,即为指数值;
  2. 确定了小数点位置之后,我们对数值字符串进行原地操作,并且前面加上“0.”的前缀;
  3. 然后从新加的小数点后面开始遍历,如果是0,说明之前是一个小于1的数,小数点需要右移,统计0的个数,同时把0移除;如果没有0,说明之前是一个大于1的数,那么其指数值是之前小数点的位置;
  4. 保留精确位,如果此时的字符串长度小于N位,需要补零,如果大于N,说明数据是足够的,把多余的部分删掉即可。
  该题结合string的erase和find函数,会大大节省代码长度,避免重复造轮子。

/*
** @Brief:No.1059 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-25
** @Solution: https://blog.csdn.net/CV_Jason/article/details/85282890
*/
#include<iostream>  
#include<string>
using namespace std;  
void getmachine(int*e, string *str,int N)
{
  int dot = (*str).find('.')  ; // 寻找小数点的位置 
  if (dot != -1)// 如果存在小数点,那么将该小数点消除 
  {
    str->erase(dot, 1);
      (*e) = dot; //同时记录小数点的位置,该位置代表了进位数 
  }
  else(*e) = str->size(); // 如果不存在小数点,说明输入的是个整数 
  str->insert(0, "0.");// 在字符的首部插入 '0.' 
  // 寻找小数点的位置 
  dot = 2;// 因为已经插入了“0.”所以,从2开始算 
  // 如果存在许多0,那么跳过这些0,并统计个数 
  while (str->size() > dot && (*str)[dot] == '0')
    dot++;
  dot -= 2;// 减去“ 0.”的长度,得到0的长度 
  str->erase(2, dot);// 消除“0.”之后所有的0 
  (*e) -= dot;// 记录指数值 
  dot = str->size();
  if (dot == 2)(*e) = 0;
  N += 2;// 开始计算保留位数 
  if (dot >  N) str->erase(N, dot - N +1);
  else str->insert(dot, N - dot, '0');
}

int main()
{
  string str[2];
  int e[2];
  int N;
  cin >> N >> str[0] >> str[1];
  getmachine(&e[0], &str[0],N);
  getmachine(&e[1], &str[1],N);
  if (e[0] == e[1] && str[0] == str[1])cout << "YES " << str[0] << "*10^" << e[0] << endl;
  else cout << "NO " << str[0] << "*10^" << e[0] << " " << str[1] << "*10^" << e[1] << endl;
  system("pause");
  return 0;
}

在这里插入图片描述

总结

  这道题看起来挺简单,其实挺难的……

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/85304491
今日推荐