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×105with 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 Bare 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
看似不难的一道题,思路很清晰,就是转换成规定的科学计数法之后比较
测试数据却是十分孤儿,最后符测试点
#include<bits/stdc++.h>
using namespace std;
string s1, s2, a1, a2;
string translate(int n, string s) {
int size = s.size(), i = 0, count = 0;
string temp;
for (; i < size&&s[i] == '0'; i++);
if (s[i] == '.') {
i++;
for (; s[i] == '0'; i++)count--;
bool judge = false; //防止0.0这样的数据,只有judge为true时,count才有效
for (; temp.size() < n&&i < size; i++) {
temp.push_back(s[i]);
judge = true;
}
while (temp.size() < n)temp.push_back('0');
if(!judge)count=0;
}
else {
for(int j=i;s[j]!='.'&&j<size;j++)count++;
for (; s[i] != '.'&&temp.size()<n&&i<size; i++)
temp.push_back(s[i]);
if (temp.size() < n)i++;
for (; i < size&&temp.size() < n; i++)
temp.push_back(s[i]);
while (temp.size() < n)temp.push_back('0');
}
temp += ("*10^" + to_string(count));
return temp;
}
int main() {
int n = 0;
scanf("%d", &n);
cin >> s1 >> s2;
a1 = translate(n, s1);
a2 = translate(n, s2);
if (a1 == a2) cout << "YES 0." << a1;
else cout << "NO 0." << a1 << " 0." << a2;
return 0;
}
测试点:
3 12300 12358.9
YES 0.123*10^5
1 12300 12358.9
YES 0.1*10^5
1 1.2300 1.23589
YES 0.1*10^1
5 1.2300 1.23589
NO 0.12300*10^1 0.12358*10^1
4 0.01234 0.012345
YES 0.1234*10^-1
5 0.01234 0.012345
NO 0.12340*10^-1 0.12345*10^-1
5 0.1234 0.12345
NO 0.12340*10^0 0.12345*10^0
0 0.11 0
YES 0.*10^0或者YES 0.0*10^0,都可以AC,测试点应该没有这个例子
1 0.1 0
NO 0.1*10^0 0.0*10^0
1 0.0 0.1
NO 0.0*10^0 0.1*10^0
1 0.0 0.000
YES 0.0*10^0
1 00.0 0.000
YES 0.0*10^0
4 00.0 0.000
YES 0.0000*10^0
5 00.0 0.000
YES 0.00000*10^0
1 05.0 5.000
YES 0.5*10^1
1 00.01 0.010
YES 0.1*10^-1