1 题目
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 A and 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
2 解析
2.1 题意
给出两个数,问它们写成保留n位小数科学计数法后,是否相等。
2.2 思路
由于科学计数法,只需获取本体部分和指数部分即可:
考虑数据本身,按整数部分是否为0,分为两种情况来讨论:
- 1 : 0. a 1 a 2 a 3 . . . 0.a_{1}a_{2}a_{3}... 0.a1a2a3...
- 2 : b 1 b 2.... b m . a 1 a 2 a 3 . . . b_{1}b{2}....b_{m}.a_{1}a_{2}a_{3}... b1b2....bm.a1a2a3...
按有效位数3为来讨论,
对于第一种情况:
由于小数点后面还有0,因此本体部分为小数点后的第一个非零位开始的前3位(即 a k a k + 1 a k + 2 a_{k}a_{k +1}a_{k+2} akak+1ak+2,其中 a k a_{k} ak是小数点后第一个非零位);
指数为小数点与第一个非零位之间0的个数的相反数(如0.001指数为-2)。实现可以,零e初值为0,在小数点后,每出现一个0就让e减1,直到到达最后一位(因为小数点后可能出现全0的情况)或者出现非零位为止。
对于第二种情况:
假设b1为从左到右第一个不为0的数,本体部分为从b1开始的三位;
指数则是小数点前数位的总位数m。
注意:数据可能出现前导0,如000.01或00123.45,为了应对这种情况要删除所有的前导零。
-
删除所有前导0后,按字符串的第一位是否是小数点,来区分情况一,还是情况二。
-
解题步骤:
- 对于情况一的预处理需要:删除前导零、小数点和第一个非零位前面的0;
- 对于情况二的预处理要:删除前导零,小数点;
- 预处理后,将剩余位数取有效部分赋值到新字符串,长度不够的有效位后面补0(在前面预处理删除的同时,可以计算指数e)。
注意:如果这个数是0,那么指数也为0;第二种情况可能存在没有小数点的情况。
3 参考代码
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int N;
string dealNum(string s, int &e){
int k = 0;//s的下标
while(s.length() > 0 && s[0] == '0'){//删除前导0
s.erase(s.begin());
}
if(s[0] == '.'){//去掉前导零后是小数点,说明s是小于1的数
s.erase(s.begin());//删除小数点
while(s.length() > 0 && s[0] == '0'){//去除第一个非零位前的0
s.erase(s.begin());
e--;
}
}else{//去掉前导零后不要时小数点,说明s是大于1的数
while (s.length() > k && s[k] != '.'){//寻找小数点
k++;
e++;
}
if(k < s.length()){//去除小数点
s.erase(s.begin() + k);
}
}
if(s.length() == 0){
e = 0;//去掉前导零后,这个数的长度为0,说明这个数是0
}
int num = 0;
k = 0;
string res;
while(num < N){
if(k < s.length()) res += s[k++];
else res += '0';
num++;
}
return res;
}
int main(int argc, char const *argv[]){
string s1, s2, s3, s4;
cin >> N >> s1 >> s2;
int e1 = 0, e2 = 0;
s3 = dealNum(s1, e1);
s4 = dealNum(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;
}