博客
关于我
1060 Are They Equal
阅读量:420 次
发布时间:2019-03-06

本文共 2899 字,大约阅读时间需要 9 分钟。

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 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 (<) 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, 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位小数的两个数字是否相等。

思路:

  我们用cnta,cntb来表示两个浮点数的小数点的位置,pa, pb来表示两个数字第一个非0的位置。indexA[], indexB[] 用来表示结果的有效位数,expA, expB用来表示指数。

Code:

1 #include 
2 3 using namespace std; 4 5 int main() { 6 int n; 7 string a, b, tempA, tempB; 8 cin >> n >> a >> b; 9 int expA, expB;10 int cntA = a.length(), cntB = b.length();11 for (int i = 0; i < a.length(); ++i) {12 if (a[i] == '.') {13 cntA = i;14 break;15 }16 }17 for (int i = 0; i < b.length(); ++i) {18 if (b[i] == '.') {19 cntB = i;20 break;21 }22 }23 24 int pA = 0, pB = 0;25 while (a[pA] == '0' || a[pA] == '.') pA++;26 while (b[pB] == '0' || b[pB] == '.') pB++;27 28 if (cntA < pA)29 expA = cntA - pA + 1;30 else31 expA = cntA - pA;32 if (pA == a.length()) expA = 0;33 34 if (cntB < pB)35 expB = cntB - pB + 1;36 else37 expB = cntB - pB;38 if (pB == b.length()) expB = 0;39 40 int indexA = 0, indexB = 0;41 while (indexA < n) {42 if (a[pA] != '.' && pA < a.length())43 tempA += a[pA++];44 else45 tempA += '0';46 indexA++;47 }48 while (indexB < n) {49 if (b[pB] != '.' && pB < b.length())50 tempB += b[pB++];51 else52 tempB += '0';53 indexB++;54 }55 if (tempA == tempB && expA == expB) {56 cout << "YES 0." << tempA << "*10^" << expA << endl;57 } else {58 cout << "NO 0." << tempA << "*10^" << expA << " 0." << tempB << "*10^"59 << expB << endl;60 }61 return 0;62 }

 

  这种题难道是不难,就是细节太多,有一点考虑不到,就会被设计好的样例卡到。(上面的代码有一组数据没有通过)

 

转载地址:http://ystuz.baihongyu.com/

你可能感兴趣的文章
为什么面试完,总是让你回去等通知?
查看>>
Java 中初始化 List 集合的 6 种方式!
查看>>
终于有人把 HTTPS 原理讲清楚了!
查看>>
别乱提交代码了,看下大厂 Git 提交规范是怎么做的!
查看>>
在滴滴和头条干了 2 年后端开发,太真实…
查看>>
Dubbo 的心跳设计,值得学习!
查看>>
送给你 12 个 Git 使用技巧!
查看>>
国人开源了一款超好用的 Redis 客户端,真香!!
查看>>
盘点 100 个最受欢迎的 Java 库!谁拔得头筹?
查看>>
使用 Redis 实现一个轻量级的搜索引擎,牛逼!
查看>>
你还在用分页?试试 MyBatis 流式查询,真心强大!
查看>>
查看 JVM 内存的几个工具,建议收藏!
查看>>
每天数十亿次请求的应用经验分享,值得参考!
查看>>
推荐一款 ES 集群可视化工具:Cerebro,简单、实用!
查看>>
你还在用命令看日志?快用 Kibana 吧,一张图片胜过千万行日志!
查看>>
Java 11 正式发布,这 8 个逆天新特性教你写出更牛逼的代码
查看>>
python进阶(3)json文件与python字典的转化
查看>>
maven将xml文件一起打包
查看>>
阿里nacos安装及使用指南
查看>>
4、ASP.NET MVC入门到精通——NHibernate构建一个ASP.NET MVC应用程序
查看>>