算法笔记练习 题解合集
题目
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.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
思路
我们要干啥?
我们要把所有千奇百怪的输入都统一成这种标准形式:0.d[1]...d[N]*10^k
实际上就是要从输入中提取到关键的字符串d[1]...d[N]
,还有指数k
。所以先定义一个结构Standard
来存放一个数字的标准形式,其中d
为关键的字符串,exp
为指数。
代码怎么写?
假设输入数据是名为input
的字符串,正式开始处理数据:
- 精度的问题先放一放(步骤 5 会处理),我们先提取出正确的
d
和exp
。注意input
可能会有无效的前导零(比如0000123.456
),所以首先把前导零都去掉; - 分类讨论,先按照
input
中存不存在小数点来分类; - 如果不存在小数点,那么
d
即为此时的input
,exp
即为此时input
的长度,进入步骤 5; - 如果存在小数点,那么小数点前面的整数部分是零吗?(或者说原数字小于 1 吗?)
a. 如果原数字大于等于 1,exp
即为小数点在input
中的下标,d
即为去掉小数点后的input
;
b. 如果原数字小于 1,由于之前我们把前导零都去掉了,input
现在应该是.xxx
的形式。
此时exp
即为小数点后面连续的0
的个数的相反数(例如.00123
的exp
是 -2,.123
的exp
是 0),d
即为去掉小数点再去掉前导零之后的input
。但是如果小数点后面全是 0,例如.00000
,那么exp
应该等于0
,d
应该是"0"
。 - 还要对
d
进行精度的调整,假设精度为sig
:
a. 若d.size() > sig
,就把d
后面砍掉,只取前sig
位的字符;
b. 若d.size() < sig
,在d
后面不断加 0 直到其长度等于sig
。 - 用这样的方法可以获取到两个数字的标准形式,就可以愉快地进行比较,输出了。
代码
#include <iostream>
#include <string>
using namespace std;
typedef struct {
string d;
int exp;
} Standard;
Standard getStandard(string input, int sig) {
Standard ret;
string digits;
while (!input.empty() && input[0] == '0')
input.erase(input.begin());
auto dotPos = input.find('.');
if (dotPos != string::npos) {
if (dotPos == 0) {
int cnt = 0;
while (dotPos+cnt+1 < input.size() && input[dotPos+cnt+1] == '0')
++cnt;
if (dotPos+cnt+1 == input.size())
ret.exp = 0;
else
ret.exp = -cnt;
} else
ret.exp = dotPos;
input.erase(input.begin() + dotPos);
} else {
ret.exp = input.size();
}
while (!input.empty() && input[0] == '0')
input.erase(input.begin());
ret.d = input;
if (ret.d.size() < sig)
ret.d.append(sig - ret.d.size(), '0');
else if (ret.d.size() > sig)
ret.d.erase(ret.d.begin() + sig, ret.d.end());
return ret;
}
void printStandard(Standard a) {
cout << "0." << a.d << "*10^" << a.exp;
}
int main() {
int sig;
string str1, str2;
cin >> sig >> str1 >> str2;
Standard a = getStandard(str1, sig), b = getStandard(str2, sig);
if (a.d == b.d && a.exp == b.exp) {
cout << "YES ";
printStandard(a);
} else {
cout << "NO ";
printStandard(a);
putchar(' ');
printStandard(b);
}
return 0;
}