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

本文共 2902 字,大约阅读时间需要 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 <bits/stdc++.h> 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/

你可能感兴趣的文章
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>