首页 > 学院 > 开发设计 > 正文

1060. Are They Equal (25)

2019-11-14 09:44:53
字体:
来源:转载
供稿:网友

1060. Are They Equal (25)

时间限制 50 ms内存限制 65536 kB代码长度限制 16000 B判题程序 Standard 作者 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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
#include<iostream>#include<string>#include<vector>#include<algorithm>#include<string.h>using namespace std;int y;string exc(string s1,int N){	string s;char c1='0';	int dot=-1,number=-1,fir=0,c=0,i;	for(i=0;i<s1.size();i++){		if(s1[i]=='.')		dot=s1.size()-1-i;		if(s1[i]!='.'&&s1[i]!='0'){			if(fir==0){				fir=1;				number=s1.size()-1-i;			}		}	}	y=number-dot;	if(y<0)	y++;	if(number==-1)	y=0;	for(i=s1.size()-1-number;c<N;i++){		if(i<s1.size())		if(s1[i]!='.'){			s+=s1[i];			c++;		}		else{}		else{			s+=c1;			c++;		}	}	return s;}int main(){	string s1,s2;	int N,i,c,l1,l2;	cin>>N;	cin>>s1>>s2;	s1=exc(s1,N);	l1=y;	s2=exc(s2,N);	l2=y;		if(s1==s2&&l1==l2){		cout<<"YES ";		cout<<"0."<<exc(s1,N)<<"*10^"<<l1; 	} 	else{		cout<<"NO ";		cout<<"0."<<exc(s1,N)<<"*10^"<<l1; 		cout<<" 0."<<exc(s2,N)<<"*10^"<<l2; 	}} 
感想:
1.因为输入有100位所以要用字符串处理
2.注意如果输入是0和0.00,要输出YES而且次数为0
3.要考虑小数,不足的位自动补0
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表