PAT 甲级 1060  Are They Equal

本文深入探讨了一道关于浮点数比较的编程题,详细解释了如何在给定有效位数的情况下,判断两个浮点数是否被视为相等。文章提供了代码实现,包括处理前导零、指数相等问题,并分享了AC代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1060 Are They Equal (25 point(s))

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.

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 10​100​​, 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]*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.

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

经验总结:

这一题,难度还可以,就是细节的处理让人摸不着头脑,题目也没有任何提示,而且题目的测试集还不严谨.....很想跟他们反馈却根本找不到反馈的地方(遇到不严谨的地方不止一次了)
首先,说说这一题的不严谨的地方吧,对于最低限度不严谨的AC的代码,如果你输入  3 123 0.123   ,竟然可以得出   这两个浮点数是相等的结论.....  很明显,是没有判断指数是否相等。
然后,就是要注意这样一个样例 3 000002.0000001 2 这两个浮点数是相等的,就是注意前导0的处理。
最后,要注意0的处理,比如输出样例 3 0 000000.0000000,应该相等,输出的时候还要按照指定形式进行输出,输出应该为,YES 0.000*10*0
就这么多啦~

AC代码

#include <cstdio>
#include <cstring>
const int maxn=110;

int find(char str[])
{
	int i;
	for(i=0;str[i]!='\0';++i)
		if(str[i]=='.')
			return i;
	return i;
}
int dispose(char a[],int n,char c[],int &pos)
{
	int num=0;
	int len=strlen(a);
	int xpos=0;
	while(a[xpos]=='0'&&xpos<pos)
		++xpos;
	for(int i=xpos;i<pos&&num<n;++i)
		c[num++]=a[i];
	if(n-num>0)
	{
		if(num==0)
		{
			xpos=pos+1;
			while(a[xpos]=='0')
				++xpos;
			for(int i=xpos;i<len&&num<n;++i)
				c[num++]=a[i];
		}
		else
			for(int i=pos+1;i<len&&num<n;++i)
				c[num++]=a[i];
	}
	if(num==0)
	{
		pos=0;
		xpos=0;
	}
	int x=n-num;
	for(int i=0;i<x;++i)
		c[num++]='0';
	c[num]='\0';
	return xpos;
}
int main()
{
	int n,apos0,bpos0;
	char a[maxn],b[maxn],coma[maxn],comb[maxn];
	scanf("%d %s %s",&n,a,b);
	int posa=find(a);
	int posb=find(b);
	apos0=dispose(a,n,coma,posa);
	bpos0=dispose(b,n,comb,posb);
	if(posa-apos0<0)
		++posa;
	if(posb-bpos0<0)
		++posb;
	if(strcmp(coma,comb)==0&&posa-apos0==posb-bpos0)
		printf("YES 0.%s*10^%d\n",coma,posa-apos0);
	else
		printf("NO 0.%s*10^%d 0.%s*10^%d\n",coma,posa-apos0,comb,posb-bpos0);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值