题目
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 × 1 0 5 0.123\times10^5 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:
ach input file contains one test case which gives three numbers N N N, A A A and B B B, where N ( ≤ 100 ) N(\le100) N(≤100) is the number of significant digits, and A A A and B B B are the two float numbers to be compared. Each float number is non-negative, no greater than 1 0 100 10^{100} 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.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
题目大意
有两个浮点数,要把他们转化为科学计数法表示,并且保留n
位小数(不按四舍五入保留,直接丢掉多余位),比较转化位科学计数法后两位数的大小。
思路
题目不难,输入的数采用字符串存储更方便,要注意题目有输入为0的测试点。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int main(){
int n;
string a, b;
vector<char> va, vb;
cin>>n>>a>>b;
int pa = -1, qa = a.length(), pb = -1, qb = b.length();
for(int i=0; i<a.length(); i++){
if(a[i] > '0' && pa == -1)
pa = i;
if(a[i] == '.')
qa = i;
if(pa != -1 && a[i] != '.' && va.size() <= n)
va.push_back(a[i]);
if(pa != -1 && qa != a.length() && va.size() == n)
break;
}
while(va.size() < n)
va.push_back('0');
for(int i=0; i<b.length(); i++){
if(b[i] > '0' && pb == -1)
pb = i;
if(b[i] == '.')
qb = i;
if(pb != -1 && b[i] != '.' && vb.size() <= n)
vb.push_back(b[i]);
if(pb != -1 && qb != b.length() && vb.size() == n)
break;
}
while(vb.size() < n)
vb.push_back('0');
int ea = qa-pa>0?qa-pa:qa-pa+1, eb = qb-pb>0?qb-pb:qb-pb+1;
if(pa == -1)
ea = 0;
if(pb == -1)
eb = 0;
int flag = 2;
for(int i=0; i<n; i++)
if(va[i] != vb[i]){
flag--;
break;
}
if(ea != eb)
flag--;
if(flag < 2){
printf("NO 0.");
for(int i=0; i<n; i++)
printf("%c", va[i]);
printf("*10^%d ", ea);
printf("0.");
for(int i=0; i<n; i++)
printf("%c", vb[i]);
printf("*10^%d\n", eb);
}
else{
printf("YES 0.");
for(int i=0; i<n; i++)
printf("%c", va[i]);
printf("*10^%d\n", ea);
}
return 0;
}