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.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
题意:输入科学计数法要保留的小数位数,输入两串字符串,用科学计数法处理这两串字符串,如果相等输出YES 和用科学计数法输出其中一串字符串,否则输出NO,分别输出处理后的两串字符串;
思路:我们要处理的字符串可能长这样,000123000,000.000123,0000123.000,00000.0000,000000,所以第一步,除去前导0,判断数是大于0,还是小于0的数,如果是大于0,找处理后字符串的小数点,小数点前有多少数,指数就是多少,不够补0,当然大于0的数也可能没有小数点,如果遍历字符串都没有找到小数点,则有多少个数,指数就是多少,如果该数小于0,我们就要去掉小数点,看看小数点后有多少个0,找出指数,不够补0,这样处理后我们就会得到一个大于0的字符串,通过printf来控制输出格式即可;
注意点:
1.测试点:000.0000,这样的数实际值是0,但是如果是用看小于点后面有多少个0来判断指数的话就是错误的
测试样例:
5 00.0 0.000
YES 0.00000*10^0
代码:
#include<iostream>
using namespace std;
int n;
string deal(string s,int &e){
while(*s.begin()=='0'&&s.length()>0){//除去前导0的情况;
s.erase(s.begin());
}
if(*s.begin()=='.'){//说明该数小于0;
s.erase(s.begin());
while(*s.begin()=='0'){//看看有多少个0,找到指数数值;
s.erase(s.begin());//删除0,保留有效数;
e--;
}
}else {//删完0后不是小数点说明是大于0的数;
while(s[e]!='.'&&e<s.length()){//大于0的数就找小数点,看看指数是多少;
e++;
}
if(s[e]=='.')s.erase(s.begin()+e);//有些大于0的数是没有小数点的,所以要判断后再选择是否删除;
}
if(s.length()==0)e=0;
int len=0;
string str;
while(len<n){//根据所要保留的位数,处理字符串s;
if(len<s.length()){
str+=s[len];
}else {
str+='0';//补充位数,s的精度不够,又要保留n个数,所以就要补0;
}
len++;
}
return str;
}
int main(){
int e1=0,e2=0;//指数;
cin>>n;//保留的数目;
string s1,s2,s3,s4;
cin>>s1>>s2;
s3=deal(s1,e1);
s4=deal(s2,e2);
if(s3==s4&&e1==e2){
printf("YES 0.%s*10^%d",s3.c_str(),e1);
}else {
printf("NO 0.%s*10^%d 0.%s*10^%d",s3.c_str(),e1,s4.c_str(),e2);
}
return 0;
}