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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
推荐指数:※※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1060
这道题本身并没有复杂的算法,只要考虑好几个case就好了。
1.数的前面有0,例如 5 003 03
2.零的情况,例如 3 0.0000 00.0
3.指数的计算要考虑周全.
#include<iostream> #include<string.h> using namespace std; #define N 200 int 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.03 for(i=0;i<strsize;i++){//check num is large than 1 or not if(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 1 is_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; }else sa[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 0 exp_num--; for(t=0;t<len;i++){ if(i<strsize){ sa[t++]=str[i]; if(str[i]>='1'&&str[i]<='9') has_num=1; } else sa[t++]='0'; } if(0==has_num)//check whether the case of 00.000000 exp_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 number exp_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; }