Skip to content

Latest commit

 

History

History
99 lines (86 loc) · 1.68 KB

1060.md

File metadata and controls

99 lines (86 loc) · 1.68 KB

1060

image.png

#include <iostream>
#include <string>
using namespace std;

int N;
string A, B;

string deal(string s, int& e) {
	// solve e
	int i = 0;
	// 去除前导 0 
	while (s.length() > 0 && s[0] == '0') {
		s.erase(s.begin());
	}
	// 去掉前导 0 后是小数点说明 s < 1
	if (s[0] == '.') {
		s.erase(s.begin());
		// 去掉小数点后面非零位前面所有的 0 
		while (s.length() > 0 && s[0] == '0') {
			s.erase(s.begin());
			e--;
		}
	}
	else {
		// 寻找小数点
		while (i < s.length() && s[i] != '.') {
			i++;
			e++;
		}
		// 如果碰到小数点则去除
		if (i < s.length()) {
			s.erase(s.begin() + i);
		}
	}
	// 如果去掉前导 0 和小数点后,s 的长度变为 0,说明 s = 0
	if (s.length() == 0) {
		e = 0;
	}
	// solve res
	string res = "";
	int num = 0;
	i = 0;
	while (num < N) {	// 精度还没到 N
		if (i < s.size()) {
			res += s[i++];
		}
		else {
			res += '0';
		}
		num++;
	}
	return res;
}

int main() {
	cin >> N >> A >> B;
	int e1 = 0, e2 = 0;
	string a = deal(A, e1);
	string b = deal(B, e2);
	if (a == b && e1 == e2) {
		cout << "YES 0." << a << "*10^" << e1 << endl;
	}
	else {
		cout << "NO 0." << a << "*10^" << e1 << " 0." << b << "*10^" << e2 << endl;
	}
	return 0;
}

/*
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

Sample Input 3:
3 12 12
Sample Output 3:
YES 0.012*10^3
*/

References