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.
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.
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.
3 12300 12358.9
YES 0.123*10^5
3 120 128
NO 0.120*10^3 0.128*10^3
题目大意:给出N,然后再给出两个非负数,要求将两个正数转换为含有N位小数的科学记数法的数,(采用去尾法)。并比较两个科学记数是否相等,如果相等,则输出YES 并输出结果,如果不不相等则输出NO并且输出这两个数。
思路:1.寻找两个数的小数点,如果是整数,小数点在整数后的一位。记小数点的脚标为pos;
2.按照题意,从前向后寻找第一位不是0,也不是小数点的数。记录其脚标。其脚标为notZero;
3,分情况当notZero==pos notZero
4.将两个数转换为0.xxxxx的形式,如果转换后的长度小于N 则末尾用0补齐。
5.取转换后的数的前n+2(含小数点的第一位0)位字符进行比较,如果两个count相等,且n+2位数也相等则输出yes否则 输出no。
坑点:1.思考时不要太贪,不要想着怎么精简代码,一步一步的考虑,不要两步并作一步走。(会扯着蛋的)。
2.考虑极端样例,输入的数可能不是规范的非负数,比如样例6 00012 与12的比较 。样例4 的 0.00与0的比较。
整理后的代码(AC):
#include
#include
#include
using namespace std;
//寻找小数点
string::size_type findDot(string &number) {
string::size_type pos = number.find('.');
if (pos == string::npos)
pos = number.size();
return pos;
}
//寻找第一个不为0的数
void findNotZero(string &number,unsigned int ¬Zero) {
unsigned int i = 0;
for (; i < number.size(); i++) {
if (number[i] != '0' && number[i] != '.') {
break;
}
}
notZero = i;
}
//将数变为0.xxxx的格式,并计算小数点移动位数
void changeNumber( int &count, string &number, unsigned int notZero,string::size_type pos) {
if (notZero < pos) {
count = pos - notZero;
number = "0." + number.substr(notZero, pos - notZero/*可能出现00012与12的比较*/) + ((pos < number.size() ? number.substr(pos + 1) : ""));
}
else if (notZero == number.size()) {
count = 0;
number = "0.0";
}
else {
count = pos - notZero + 1;
number = "0." + number.substr(notZero);
}
}
//将变换后的数补齐到n个小数位
void appendZero(string &number,unsigned int n) {
while (number.size() <= n + 2) {
number.append("0");
}
number = number.substr(0, n + 2);
}
int main() {
unsigned int n;
string number1, number2;
cin >> n >> number1 >> number2;
//寻找小数点
string::size_type pos1 = findDot(number1);
string::size_type pos2 = findDot(number2);
//寻找第一个不为0的数
unsigned int notZero1, notZero2;
findNotZero(number1, notZero1);
findNotZero(number2, notZero2);
//将数变为0.xxxx的格式,并计算小数点移动位数
int count1, count2;
changeNumber(count1, number1, notZero1, pos1);
changeNumber(count2, number2, notZero2, pos2);
//将变换后的数补齐到n个小数位
appendZero(number1, n);
appendZero(number2, n);
//输出
if (number1 == number2 && count1 == count2) {
cout << "YES" << " " << number1 << "*10^" << count1 << endl;
}
else {
cout << "NO" << " " << number1 << "*10^" << count1 << " " << number2 << "*10^" << count2 << endl;
}
}
整理前的代码(AC):
#include
#include
#include
using namespace std;
int main() {
int n;
string number1, number2;
cin >> n >> number1 >> number2;
int len1 = number1.size(), len2 = number2.size();
string::size_type pos1 = number1.find('.');
string::size_type pos2 = number2.find('.');
if (pos1 == string::npos)
pos1 = number1.size();
if (pos2 == string::npos)
pos2 = number2.size();
int notZero1, notZero2;
int i = 0;
for (;i < number1.size();i++) {
if (number1[i] !='0' && number1[i] != '.') {
break;
}
}
notZero1 = i;
for (i = 0;i < number2.size();i++) {
if (number2[i] !='0' && number2[i] != '.') {
break;
}
}
notZero2 = i;
int count1, count2;
if (notZero1 < pos1) {
count1 = pos1-notZero1;
number1 = "0." + number1.substr(notZero1, pos1-notZero1) + ((pos1 < len1 ? number1.substr(pos1 + 1) : ""));
}
else if (notZero1 ==len1) {
count1 = 0;
number1 = "0.0";
}
else {
count1 = pos1 - notZero1 + 1;
number1 = "0." + number1.substr(notZero1);
}
if (notZero2 < pos2) {
count2 = pos2 - notZero2;
number2 = "0." + number2.substr(notZero2, pos2-notZero2) + ((pos2 < len2 ? number2.substr(pos2 + 1) : ""));
}
else if (notZero2==len2) {
count2 = 0;
number2 = "0.0";
}
else {
count2 = pos2 - notZero2 + 1;
number2 = "0." + number2.substr(notZero2);
}
while (number1.size() <= n + 2) {
number1.append("0");
}
while (number2.size() <= n + 2) {
number2.append("0");
}
number1 = number1.substr(0, n + 2);
number2 = number2.substr(0, n + 2);
if (number1 == number2&&count1 == count2) {
cout << "YES" << " " << number1 << "*10^" << count1 << endl;
}
else {
cout << "NO" << " " << number1 << "*10^" << count1 << " " << number2 << "*10^" << count2 << endl;
}
}