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
×
1
0
5
0.123×10^5
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
1
0
100
10^100
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 ,比如给出0012345,000.0124。
分两种情况:
①
0.
a
1
a
2
a
3
a
4
a
5
a
6
.
.
.
.
.
0.a_1a_2a_3a_4a_5a_6.....
0.a1a2a3a4a5a6.....
②
b
m
b
m
−
1
.
.
.
.
.
b
3
b
2
b
1
.
a
1
a
2
a
3
a
4
a
5
a
6
.
.
.
.
.
b_mb_{m-1}.....b_3b_2b_1.a_1a_2a_3a_4a_5a_6.....
bmbm−1.....b3b2b1.a1a2a3a4a5a6.....
#include<string>
#include<iostream>
#include<utility>
using namespace std;
pair<string,int> change(const string& a) {
string result="0.";
int K=0;
int len = a.size(),i=0;
for (; i < len; i++) { //跳过前面的0
if (a[i] != '0') {
break;
}
}
//now a[i] may '.' or number
//a maybe is 0000,
if (i != len) {
if (a[i] == '.') { // .
bool allzero = true;
for (i++; i < len; i++) {
if (a[i] == '0')
K--;
else {
allzero = false;
break;
}
}
// a maybe 000.0000
if (i != len)
result += a.substr(i);
if (allzero)
K = 0;
}
else { // number
bool flag = false;
for (; i < len; i++) {
if (a[i] != '.') {
result += a[i];
if (!flag)
K++;
}
else
flag = true;
}
}
}
return make_pair(result,K);
}
int main()
{
int N;
string A, B;
pair<string, int> PA, PB;
cin >> N >> A >> B;
PA = change(A);
PB = change(B);
PA.first.resize(N+2,'0');
PB.first.resize(N+2, '0');
if (PA.first == PB.first && PA.second==PB.second) {
cout << "YES ";
cout << PA.first << "*10^" << PA.second<<endl;
}
else {
cout << "NO ";
cout << PA.first << "*10^" << PA.second <<" ";
cout << PB.first << "*10^" << PB.second << endl;
}
}