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.d1...dN*10^k" (d1>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
来源: <http://www.patest.cn/contests/pat-a-practise/1060>
#include<string>
#include <iostream>
using namespace std;
int n;
int SearchDot(string s) { //Search the pos of ‘.‘
for (int i = 0; i < s.length(); i++) {
if (s[i] == ‘.‘)
return i;
}
return -1;
}
string ChangeToSci(string s1) {
string s1new;
bool isZero = true;//check whether it is 0
for (int i = 0; i < s1.length(); i++) {
if (s1[i] != ‘0‘&&s1[i] != ‘.‘)
isZero = false;
}
if (isZero) {
s1new += "0.";
for (int i = 0; i < n; i++)
s1new += ‘0‘;
s1new += "*10^0";
return s1new;//if 0 return
}
bool hengji = false;
string temp;
for (int i = 0; i < s1.length()-1; i++) {//remove 0 in the head such as 05.02
if (s1[i+1]>=‘0‘&&s1[i+1]<=‘9‘&&s1[i] == ‘0‘&&hengji == false) {
continue;
}
else {
hengji = true;
}
temp += s1[i];
}
temp += s1[s1.length() - 1];
s1 = temp;
int dotPos = SearchDot(s1);
if (dotPos == -1) {//no dot in the number ,such like 12345
s1new += "0.";
for (int i = 0; i < n; i++) {
if (i < s1.length())
s1new += s1[i];
else
s1new += ‘0‘;
}
s1new += "*10^";
int temp = s1.length();
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + ‘0‘;
s1new += temp % 10 + ‘0‘;
}
else
s1new += temp + ‘0‘;
}
else if (dotPos != 1) {//dot not at the second pos, such like 123.45 12.3
s1new += "0.";
bool dotFlag = false;
for (int i = 0; i < n; i++) {
if (i < s1.length()) {
if (s1[i] == ‘.‘) {
dotFlag = true;
continue;
}
s1new += s1[i];
}
else
s1new += ‘0‘;
}
if (dotFlag == true) {
if (n < s1.length())
s1new += s1[n];
else
s1new += ‘0‘;
}
s1new += "*10^";
int temp = dotPos;
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + ‘0‘;
s1new += temp % 10 + ‘0‘;
}
else
s1new += temp + ‘0‘;
}
else {
if (s1[0] == ‘0‘) {//0.23 0.00023 and so on
s1new += "0.";
int j = 2, cnt = 0, flag = 0;
while (true)
{
if (s1[j] == ‘0‘ && flag == 0)
cnt++;
else
break;
j++;
}
for (int i = j; i < j + n; i++) {
if (i < s1.length())
s1new += s1[i];
else
s1new += ‘0‘;
}
if (cnt == 0) {
s1new += "*10^0";
}
else {
s1new += "*10^-";
int temp = cnt;
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + ‘0‘;
s1new += temp % 10 + ‘0‘;
}
else
s1new += temp + ‘0‘;
}
}
else {//2.002 1.354 and so on
s1new += "0.";
for (int i = 0; i <= n; i++) {
if (i == 1)
continue;
if (i < s1.length())
s1new += s1[i];
else
s1new += ‘0‘;
}
s1new += "*10^1";
}
}
return s1new;
}
int main(void) {
string s1, s2;
string s1new, s2new;
cin >> n >> s1 >> s2;
if (n == 0) {
cout << "YES 0";
return 0;
}
string s11, s22;
s11 = ChangeToSci(s1);
s22 = ChangeToSci(s2);
if (s11 == s22) {
cout << "YES " << s11;
}
else {
cout << "NO " << s11 << " " << s22;
}
return 0;
}