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 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 , and , where () is the number of significant digits, and and are the two float numbers to be compared. Each float number is non-negative, no greater than , 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
17/25
#include <iostream>
#include <string>
#include <algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int N;//精度
string A;
string B;
bool is_zero(string a) {//判断0
if (a.length() == 1 && a[0] == 0) {
return 1;
}
return 0;
}
string to_form(int &index, string a) {//这里不考虑a=0
//int index = 0;//记录指数
int len = a.length();
string str;//记录去0后的数
if (a[0] == '0') {//处理0.0000000...这类数
for (int i = 0; i < len; i++) {
if (a[i] >'0'&&a[i]<'9') {
index = -(i - 2);
str = a.substr(i, len - i);
break;
}
}
}
else {//处理123456.789......和12345600......
for (int i = 1; i < len; i++) {
if (a[i] == '.') {
index = i;
str = a.substr(0, 5) + a.substr(i + 1, len - i + 1);
break;
}
else if(i==len-1){//最后一位肯定不是小数点,所有只有整数才能遍历到最后一位
index = len;
str = a;
}
}
}
return "0." + str;
}
string deal(string a, int n,int &index) {//输出格式化
string str;
if (is_zero(a) == true) {
for (int i = 0; i < n; i++) {
str += '0';
}
str = "0." + str;
return str;
}
else {
str = to_form(index, a);
if (str.length() < (n + 2)) {//位数不足后面补零
for (int i = 0; i < (n + 2 - str.length()); i++) {
str = str + '0';
}
}
if (str.length() > (n + 2)) {//位数过长截尾
str.erase(n + 2, str.length() - (n + 2));
}
return str;
}
}
int main() {
cin >> N;
cin >> A;
cin >> B;
int index_a = 0;
int index_b = 0;//a,b指数
string s_a;
string s_b;
s_a = deal(A,N,index_a);
s_b = deal(B, N,index_b);
//cout << s_a << "*10^" << index_a;
if (s_a == s_b&&index_a==index_b) {
cout << "YES " << s_a<<"*10^"<<index_a;
}
else {
cout << "NO " << s_a <<"*10^"<<index_a<< ' ' << s_b<<"*10^"<<index_b;
}
}



最新评论