PAT A1060 Are They Equal

PAT A1060 Are They Equal

题目描述
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×10^5​​ 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.
输入
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 10的​100次方​​ , and that its total digit number is less than 100.
输出
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]*10^k (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.
测试样例

4 0000 0000.00 //YES 0.0000*10^-0
4 00000.000000123 0.0000001230 //YES 0.1230*10^-6
3 123 0.12345// NO 0.123*10^3 0.123*10^0

思路
1、首先找到string中小数点的位置,若未找到在字符串末尾加上“.”;
2、接着记录从string起始到小数点位置,第一个不为“0”的数的位置 i,记录从小数点后一位开始到string结束的第一个不为“0”的位置 j。(目的是,保留化为标准形式后小数点以后的数字,去掉)
3、若i小于小数点的位置,则从i到结束都是有效数字,其次方为小数点位置与i的差;
若i不小于小数点的位置,则还要就要利用j ,从j到结束都是有效数字,其次方为小数点位置与i的差+1;
仅保留有效数字和次方。
4 、若有效数字和次方都想同则YES。

AC代码

#include<iostream>
#include<string>
using namespace std;

struct Fnum
{
	string f; //仅包含有效数字的串
	int dot;//小数点的位置
	int p;//次方
};

Fnum Tonormal(string a,int n)
{
	Fnum ans;
	int i=0,j;

	ans.dot=a.find("."); //找到小数点的位置

	if(ans.dot== string::npos) //若不存在,则在末尾加上
	{
		ans.dot=a.length();
		a+=".";
	}

	while(i<ans.dot)   //找到从string起始到小数点位置,第一个不为“0”的数的位置 i
	{    
		if(a[i]!='0')break;
		i++;
	}
	
	j=ans.dot+1;    //记录从小数点后一位开始到string结束的第一个不为“0”的位置 j
	while(j<a.length())
	{
		if(a[j]!='0')break;
		j++;
	}

	if(i<ans.dot)   //若小数点前存在有效数字
	{
		if(ans.dot==a.length())   // 若小数点在串的最后,就不用再加小数点后面的东西了
		{
			ans.f=a.substr(i,ans.dot-i);
			ans.p=ans.dot-i;
		}
		else  // 若小数点后面还有东西,则加上后面的串
		{
			ans.f=a.substr(i,ans.dot-i)+a.substr(ans.dot+1,a.length()-ans.dot);
			ans.p=ans.dot-i;
		}

	}
	else if(j<a.length())  //若小数点前面没有有效数字,就要定位筛选小数点后的有效数字
	{
		ans.f=a.substr(j,a.length()-j);   
		ans.p=ans.dot-j+1;
	}
	else 
	{
		ans.f="0";  //若小数点前后都没有有效数字就只能是0了
		ans.p=0;
	}
	while(ans.f.length()<=n)  ans.f+="0";  //若精度大于有效数字个数,则在末尾补零

	return ans;
}



int main()
{
	int n;
	string a,b;
	cin>>n>>a>>b;

	Fnum f1=Tonormal(a,n);  
	Fnum f2=Tonormal(b,n);
	//注意!!!不要忽略次方相等
	if(f1.f.substr(0,n) == f2.f.substr(0,n) && f1.p==f2.p ) cout<<"YES 0."<<f1.f.substr(0,n)<<"*10^"<<f1.p<<endl;
	else  cout<<"NO 0."<<f1.f.substr(0,n)<<"*10^"<<f1.p<<" 0."<<f2.f.substr(0,n)<<"*10^"<<f2.p<<endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值