PAT 1060. is they Equal (25)

Source: Internet
Author: User
Tags float number

1060. is they Equal (25)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 is considered equal since they is B Oth saved as 0.123*105 with simple chopping. Now given the number of significant digits in a machine and both float numbers, you is supposed to tell if they is treate D equal in this 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 Significan t digits, and A and B are the numbers of the float to be compared. Each of the float number is non-negative, the no greater than 10100, and that it total digit number are less than 100.

Output Specification:

For each test case, print with a line "YES" If the numbers is 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 is not a treated equal, and then the "numbers in their" form. All the terms must is 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

The things to note are 0.00, 0012.3, 0.00123, etc.

1#include <iostream>2#include <string>3 4 using namespacestd;5 6 intCalculateradix (string&num)7 {8     inti =0;9      while(I <num.size ())Ten     { One         if(Num[i] = ='.') A         { -Num.erase (I,1); -              Break; the         } -i++; -     } -  +     returni; - } +  A intErasezero (string&num) at { -     intCNT =0; -     string:: Iterator it =Num.begin (); -      while(It!=num.end () && (*it) = ='0') -     { -it =Num.erase (it); incnt++; -     } to     returnCNT; + } -  the stringCalculatecoeff (stringNumintSignificantnum,int&Radix) * { $     stringCoeff;Panax NotoginsengRadix-=Erasezero (num); -     if(Num.empty ()) theRadix =0; +     if(Num.size () >=significantnum) ACoeff.assign (Num.begin (), Num.begin () +significantnum); the     Else +Coeff = num +string(Significantnum-num.size (),'0'); -  $     return "0."+Coeff; $ } -  - intMain () the { -     stringnum1, num2;Wuyi     intSignificantnum; theCIN >> Significantnum >> NUM1 >>num2; -  Wu     intradix1, radix2; -RADIX1 =Calculateradix (NUM1); AboutRadix2 =Calculateradix (num2); $      -     stringCOEFF1 =Calculatecoeff (NUM1, Significantnum, radix1); -     stringCOEFF2 =Calculatecoeff (num2, Significantnum, radix2); -  A     if(coeff1 = = Coeff2&&radix1 = =radix2) +cout <<"YES"<<" "<< coeff1 <<"*10^"<<radix1; the     Else -cout <<"NO"<<" "<< coeff1 <<"*10^"<< radix1 <<" "<< coeff2 <<"*10^"<<radix2; $}

PAT 1060. is they Equal (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.