PAT1060 Are They Equal (25 分)

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 ∗ 1 0 5 0.123*10^5 0.123105with 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 ) N (<100) 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 than10100, 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 ] ∗ 1 0 k ( d [ 1 ] > 0 0.d[1]...d[N]*10^k (d[1]>0 0.d[1]...d[N]10k(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.

#include <bits/stdc++.h>
using namespace std;
char a[101],b[101];
vector<char> v1,v2;
int main() {
	int n,i,p1=-1,n1=-1,p2=-1,n2=-1,l1=-1,l2=-1,f1=0,f2=0,ff1=0,ff2=0;
	scanf("%d %s %s",&n,a,b);
	for(i=0;i<100;++i){
		if(a[i]=='.')p1=i;
		if(b[i]=='.')p2=i;
		if(a[i]<='9'&&a[i]>'0'&&f1==0){f1=1;n1=i;}
		if(b[i]<='9'&&b[i]>'0'&&f2==0){f2=1;n2=i;}
		if(a[i]<46&&ff1==0)l1=i,ff1=1;
		if(b[i]<46&&ff2==0)l2=i,ff2=1;
		if(a[i]<46&&b[i]<46)break;		
	}
	int temp[n];fill(temp,temp+n,'0');
	if(n1==-1){v1.insert(v1.begin(),temp,temp+n);p1=n1;}
	else{
		if(l1-n1-(p1>n1?1:0)<n){//zero
			if(p1>n1){
				v1.insert(v1.begin(),a+n1,a+p1);
				v1.insert(v1.end(),a+p1+1,a+l1);
			}else v1.insert(v1.begin(),a+n1,a+l1);		
		}
		else{
 			if(p1>n1&&p1<n1+n){//no zero
				v1.insert(v1.begin(),a+n1,a+p1);
				v1.insert(v1.end(),a+p1+1,a+n1+n+1);		
			}
			else v1.insert(v1.begin(),a+n1,a+n1+n);
		}
		if(p1==-1)p1=l1;
	}
	if(n2==-1){v2.insert(v2.begin(),temp,temp+n);p2=n2;}
	else{
		if(l2-n2-(p2>n2?1:0)<n){
			if(p2>n2){
				v2.insert(v2.begin(),b+n2,b+p2);
				v2.insert(v2.end(),b+p2+1,b+l2);
			}else v2.insert(v2.begin(),b+n2,b+l2);
		}
		else{
			if(p2>n2&&p2<n2+n){
				v2.insert(v2.begin(),b+n2,b+p2);
				v2.insert(v2.end(),b+p2+1,b+n2+n+1);
			}
			else v2.insert(v2.begin(),b+n2,b+n2+n);
		} 
		if(p2==-1)p2=l2;
	}
	if(v1==v2&&p1-n1==p2-n2){
		printf("YES 0.");
		for(i=0;i<v1.size();++i)printf("%c",v1[i]);
		printf("*10^%d",p1-n1+(p1<n1&&p1>-1?1:0));
	}
	else{
		printf("NO 0.");
		for(i=0;i<v1.size();++i)printf("%c",v1[i]);
		printf("*10^%d 0.",p1-n1+(p1<n1&&p1>-1?1:0));
		for(i=0;i<v2.size();++i)printf("%c",v2[i]);
		printf("*10^%d",p2-n2+(p2<n2&&p2>-1?1:0));
	} 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值