1060. Are they equal (25)-pat

Source: Internet
Author: User
Tags float number
1060. Are they equal (25) Time Limit 50 ms memory limit 32000 kb code length limit 16000 B discriminant program standard author Chen, Yue

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
Recommendation index :※※
Source: http://pat.zju.edu.cn/contests/pat-a-practise/1060
This question does not have a complex algorithm. You only need to consider several cases.
1. There is 0 in front of the number, for example, 5 003 03
2. Zero case, such as 3 0.0000 00.0
3. The calculation of indexes should be well considered.
#include<iostream>#include<string.h>using namespace std;#define  N 200int formatstr(char *str,char *sa,int len,int strsize){int i;int exp_num=0;int isbig=0,first_use=0;//first_use is the first use number,because of the case may have such 003 00.03for(i=0;i<strsize;i++){//check num is large than 1 or notif(str[i]>='1'&&str[i]<='9'){first_use=i;isbig=1;break;}else if(str[i]=='.'){first_use=i;isbig=0;break;}}int t,is_point_done,has_num;if(isbig==1){// num is large than 1is_point_done=0;for(i=first_use,t=0;t<len;i++){if(i<strsize){if(str[i]>='0'&&str[i]<='9'){sa[t++]=str[i];if(0==is_point_done)exp_num++;}else if(str[i]=='.')is_point_done=1;}elsesa[t++]='0';}for(;i<strsize&&is_point_done==0;i++){//check number whether arrive point '.' if(str[i]=='.')break;exp_num++;}}else{has_num=0;for(i=first_use+1;i<strsize&&str[i]=='0';i++)//skip 0exp_num--;for(t=0;t<len;i++){if(i<strsize){sa[t++]=str[i];if(str[i]>='1'&&str[i]<='9')has_num=1;}elsesa[t++]='0';}if(0==has_num)//check whether the case of 00.000000exp_num=0;}sa[t]='\0';return exp_num;}void print_num(char *a,int len,int exp_num){int i;cout<<"0.";for(i=0;i<len;i++)cout<<a[i];cout<<"*10^"<<exp_num;}int main(){int i,n,exp_num_a,exp_num_b;char a[N],b[N];char sa[N],sb[N];cin>>n>>a>>b;exp_num_a=formatstr(a,sa,n,strlen(a));//format for two numberexp_num_b=formatstr(b,sb,n,strlen(b));if(exp_num_a==exp_num_b){for(i=0;i<n;i++){if(sa[i]!=sb[i]){cout<<"NO ";print_num(sa,n,exp_num_a);cout<<" ";print_num(sb,n,exp_num_b);break;}}if(i==n){cout<<"YES ";print_num(sa,n,exp_num_a);}}else{cout<<"NO ";print_num(sa,n,exp_num_a);cout<<" ";print_num(sb,n,exp_num_b);}return 0;}

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.