1060 Are They Equal

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.12010^3 0.12810^3

题目大意:让你用科学计数法表示两个数字,然后判断用科学计数法表示的两个数字是否相等,其中n为有效数字位数。

大致思路:

  1. 先排除前导0的影响,删除当前字符串的所有前导0,然后在判断当前字符串是>1 还是<1.
  2. 如果>1,设置一个指针K遍历当前字符串,直到找到小数点或者遍历到字符串的最后一位。然后删除小数点,同时在遍历的时候指数部分e也要不断增加
  3. 如果<1, 设置一个指针k遍历当前字符串,不断删除小数点后面的前导0,同时指数部分e不断-1.
  4. 上述过程处理完之后,在判断当前字符串的长度和n的大小,如果<n说明后面要补0

代码:

#include <bits/stdc++.h>

using namespace std;

int n;

string changeNum(string str, int& e) {
int k = 0; //str的下标
while(str.length() > 0 && str[0] == '0')
str.erase(str.begin()); //先去掉前导0
//如果是小数
if (str[0] == '.') {
str.erase(str.begin());
//去掉非0位前面的所有0
while(str.length() > 0 && str[0] == '0') {
str.erase(str.begin());
e--; //小数,指数依次递减
}
}
//如果是整数
else {
while(k < str.length() && str[k] != '.') {
k++;
e++;
}
if (k < str.length()) str.erase(str.begin() + k);
} //说明这个数为0
if (str.length() == 0) e = 0; //指数为0
int num = 0; k = 0;
string ans = "";
while(num < n) {
if (k < str.length()) ans += str[k++];
else ans += '0'; //如果超过表示范围后面要补0
num++;
}
return ans;
} int main() {
string str1, str2;
cin >> n >> str1 >> str2;
int e1 = 0, e2 = 0;
string ans1 = changeNum(str1, e1), ans2 = changeNum(str2, e2);
if (ans1 == ans2 && e1 == e2) cout << "YES 0." << ans1 << "*10^" << e1 << endl;
else cout << "NO 0." << ans1 << "*10^" << e1 << " " << "0." << ans2 << "*10^" << e2 << endl;
return 0;
}

最新文章

  1. poj 1626
  2. 设计模式之美:Null Object(空对象)
  3. 朝花夕拾-android 一个注册新用户时,多步填写用户资料的框架
  4. GROUPING SETS、CUBE、ROLLUP
  5. oracle中DECODE与CASE的用法区别
  6. 详解 MySQL 中的 explain
  7. 浅谈zygote服务中的设计思路
  8. Linux GCC
  9. Nginx + IIS实现负载均衡 Session多站点共享
  10. [物理学与PDEs]第3章第4节 磁流体力学方程组的数学结构
  11. 台式电脑、笔记本快捷选择启动项Boot 快捷键大全
  12. Java高级特性 第5节 序列化和、反射机制
  13. cf796d 树,bfs好题!
  14. Thinkphp5 关联模型
  15. JAVA⑤
  16. 谨慎注意WebBrowser控件的DocumentCompleted事件
  17. Effective Java(1)-创建和销毁对象
  18. Windows下用HackRF和SDR#收听FM
  19. ELK日志分析平台搭建全过程
  20. python学习笔记:第9天 函数初步

热门文章

  1. 4. Linux工作目录切换和文本文件编辑命令
  2. this.$nextTick( 回调函数 )的作用
  3. The 10th Shandong Provincial Collegiate Programming Contest(11/13)
  4. 【uva 1153】Keep the Customer Satisfied(算法效率--贪心+优先队列)
  5. 高斯消元初步(Gauss算法)
  6. POJ_2112 二分图多重匹配
  7. Caocao&#39;s Bridges HDU - 4738 找桥
  8. Jpress小程序
  9. K8s Deployment YAML 名词解释
  10. Linux虚拟机封装成模板