1.题目要求把两个数分别转换成一定精度的数,用科学计数法显示,然后判断两个数是否相等。
2.下面列出了一些测试点,通过这些测试点,也就可以AC了:
[c language=”++”]
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
[/c]
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
AC代码,一定要自己写一遍才知道坑所在:
[c language=”++”]
//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
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
*/
bool isNum(char c)
{
return (c <= ‘9’&&c >= ‘0’);
}
string fixN(int n, string s)
{//如果不足N位,则补0,如果超过n位,则截断
if (n == 0) return "0";
if (s.size() >= n)
return s.substr(0, n);
else
{
//return s;//不补0,也可以
int size = s.size();//在这里卡住了,本来下面的条件是i<n-s.size()+1,这样的话随着s补零,s的长度发生变化
for (int i = 0; i < n – size; i++)
s += ‘0’;
return s;//补0,也可以
}
}
string num2String(int a)
{
if (a == 0) return "0";
string ans = "";
while (a != 0)
{
char c = a % 10 + ‘0’;
ans += c;
a /= 10;
}
return ans;
}
string trans(int n, string str)
{
if (str == "0")
{
string tmp = "";
for (int i = 0; i < n – str.size() + 1; i++)
tmp += ‘0’;
if (tmp == "") tmp = "0";
return str + "." + tmp + "*10^0";
}
else if (str[0] == ‘0’)
{//小数:0.1 0.0001
int p = 0;
string tmp = "";
for (int i = 0; i < str.size(); i++)
{
if (isNum(str[i]))
tmp += str[i];
}
tmp = tmp.substr(1);//除去整数位的0
for (int i = 0; i < tmp.size(); i++)
{//0.00001 有四个连续的0
if (tmp[i] == ‘0’)
p++;//计算前面有多少个连续的0
else break;
}
tmp = tmp.substr(p);
if (tmp == "") p = 0;//如0.0,此时的tmp为0,p为1,截掉后tmp为空,p还有1,p应为0
tmp = fixN(n, tmp);
if (p == 0)
return "0." + tmp + "*10^" + num2String(p);
else
return "0." + tmp + "*10^-" + num2String(p);
}
else
{//大于1的数
int p = -1;//小数点位置
string tmp = "";
for (int i = 0; i < str.size(); i++)
{
if (isNum(str[i]))
tmp += str[i];
if (p == -1 && str[i] == ‘.’)
p = i;//求得小数点的位置
}
if (p == -1)//str多少位,就有多少个10,如1234
p = str.size();
tmp = fixN(n, tmp);
string ans = "0." + tmp + "*10^" + num2String(p);
return ans;
}
}
string deleteZero(string a)
{
int p = 0;
for (int i = 0; i < a.size() – 1; i++)
{
if (a[i] == ‘0’&&a[i + 1] != ‘.’)
p++;
else
break;
}
return a.substr(p);
}
int main(void)
{
int n;
string a, b;
cin >> n >> a >> b;
//if (n == 0)
//{//这部分输出YES 0.*10^0或者YES 0.0*10^0,都可以AC,测试点应该没有这个例子
// cout << "YES 0.0*10^0" << endl;
// return 0;
//}
string a2 = trans(n, deleteZero(a));
b = deleteZero(b);
string b2 = trans(n, b);
if (a2 == b2)
cout << "YES " << a2 << endl;
else
cout << "NO " << a2 << " " << b2 << endl;
return 0;
}
[/c]