# 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 思路

• 1 ： 0. a 1 a 2 a 3 . . . 0.a_{1}a_{2}a_{3}...
• 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}...

• 删除所有前导0后，按字符串的第一位是否是小数点，来区分情况一，还是情况二。

• 解题步骤：

• 对于情况一的预处理需要：删除前导零、小数点和第一个非零位前面的0；
• 对于情况二的预处理要：删除前导零，小数点；
• 预处理后，将剩余位数取有效部分赋值到新字符串，长度不够的有效位后面补0(在前面预处理删除的同时，可以计算指数e)。

# 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;
}


12-19 127

03-04 97
03-02 164
01-11 227
08-25 1996
02-28 805