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*10^5^ 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 10^100^, 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
题目大意:给定有效位数, 判断两个浮点数是否相同, 并且输出浮点表示
思路:找到第一个非0数字的位置a0,以及小数点的位置adot, 如果a0>adot,则指数expa=a0-adot, 否则expa=a0-adot+1。删除小数点,以及有效位之前的0; 根据有效数字增加0,或者截断数字
注意点:当数字是0的时候指数一定的0,不管小数点在什么位置。 还有就是除了数字相同,还要指数相同的两个数字才是相等的。 开始没有考虑指数是负数需要加1,导致有一个点不能通过
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main(){ 5 int n, i; 6 string s1, s2; 7 cin>>n>>s1>>s2; 8 ////////////////////////////////////// 9 //确定每个数的指数 10 int adot=-1, bdot=-1, aFirstNoneZero=-1, bFirstNoneZero=-1; 11 for(i=0; i<s1.size(); i++) if(s1[i]=='.') adot=i; 12 for(i=0; i<s1.size(); i++) if(s1[i]>'0'&& s1[i]<='9' && aFirstNoneZero==-1) aFirstNoneZero=i; 13 for(i=0; i<s2.size(); i++) if(s2[i]=='.') bdot=i; 14 for(i=0; i<s2.size(); i++) if(s2[i]>'0' && s2[i]<= '9' && bFirstNoneZero==-1) bFirstNoneZero=i; 15 if(adot!=-1) s1.erase(adot, 1); 16 if(bdot!=-1) s2.erase(bdot, 1); 17 adot = adot==-1 ? s1.size() : adot; 18 bdot = bdot==-1 ? s2.size() : bdot; 19 int apow=adot - aFirstNoneZero, bpow=bdot - bFirstNoneZero; 20 apow = apow>0 ? apow : apow+1; 21 bpow = bpow>0 ? bpow : bpow+1; 22 ////////////////////////////////////////////// 23 //删除无意义的0 24 aFirstNoneZero=0; bFirstNoneZero=0; 25 while(aFirstNoneZero<s1.size() && s1[aFirstNoneZero]=='0') aFirstNoneZero++; 26 while(bFirstNoneZero<s2.size() && s2[bFirstNoneZero]=='0') bFirstNoneZero++; 27 s1.erase(0, aFirstNoneZero); 28 s2.erase(0, bFirstNoneZero); 29 /////////////////////////////////////// 30 //添加或删除子串 让精度正确 31 int len; 32 if(s1.size()>n) s1 = s1.substr(0, n); 33 while(s1.size()<n) s1.append("0"); 34 if(s2.size()>n) s2 = s2.substr(0, n); 35 while(s2.size()<n) s2.append("0"); 36 /////////////////////// 37 //数字是0的情况 38 apow = s1[0]=='0' ? 0 : apow; 39 bpow = s2[0]=='0' ? 0 : bpow; 40 if(s1==s2 && apow==bpow) cout<<"YES 0."<<s1<<"*10^"<<apow; 41 else cout<<"NO 0."<<s1<<"*10^"<<apow<<" 0."<<s2<<"*10^"<<bpow<<endl; 42 return 0; 43 }