PAT_1060: 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*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.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
备注:不难,就是要考虑的方面太多了。1)注意数字前多余的0要去掉;2)对于0的特殊处理;3)对于大于1的数和小于1大于0的数处理方法不同。刚开始时没有转换成科学计数法直接比较,只过了3个case;后来改成了转换完后直接比较,既方便又不容易搞错。
各种要求的计数格式:(有效位数 原数 要求的计数格式)
3	0		0.000*10^0
5	0.000000		0.00000*10^0
3	0.1		0.1*10^0
4	0.000010100	0.1010*10^-4
5	1.000201		0.10002*10^1
 
#include<stdio.h>
#include<string.h>

#define MAXSIZE 101

//judge whether the number is 0
int IsZero(char s[])
{
	if(s[0]=='0' && strlen(s)==1) // s="0"
		return 1;
	else
	{
		for(int i=0;s[i]!='\0';i++)
			if(s[i]!='0' && s[i]!='.')
				return 0;
	}
	return 1; // s="0.0000000"
}

//judge whether the number is less than 1, like 0.XXXXXXXXXXXX (0<X<=9)
int IsLessThanOne(char s[]) 
{
	if(s[0]=='0' && s[1]=='.')
		return 1;
	return 0;
}

//judge whether the number is a float point
int HasPoint(char s[])
{
	for(int i=0;s[i]!='\0';i++)
	{
		if(s[i]=='.')
			return 1;
	}
	return 0;
}

//chop the leading zero before the number, store the result in string t[]
void ChopLeadingZeros(char s[],char t[])
{
	if(IsZero(s)) // s="0" or "0.000000000000"
	{
		strcpy(t,s);
		return;
	}
	if(s[0]!='0') // s="123" or "123.45"
	{
		strcpy(t,s);
		return;
	}

	int j=0, HasChopped=0;
	for(int i=0;s[i]!='\0';i++)
	{
		if(!HasChopped)
		{
			if((s[i]!='0') || (s[i]=='0' && s[i+1]=='.'))
			{
				t[j++]=s[i];
				HasChopped=1;
			}
		}
		else
			t[j++]=s[i];
	}
	t[j]='\0';
} 

// for a number >=1, Count the number of digits before "."
// for a number <1, count the number of zeros before the first non-zero digit
int CountBeforePoint(char s[])
{
	int count=0;
	if(IsZero(s)) // s="0" or "0.000000000000"
		return 0;
	if(IsLessThanOne(s)) // 0.XXXXXXX(0.1XXXXXX returns 0, 0.0003XXXXX returns 3
	{
		for(int i=0;s[i]!='\0';i++)
		{
			if(s[i]=='0')
				count++;
			else if(s[i]!='0' && s[i]!='.')
				break;
		}
		return count-1;
	}

	for(int i=0;s[i]!='\0';i++) // 123000 or 123.45
	{
		if(s[i]=='.')
			break;
		else
			count++;
	}
	return count;
}
// transform a number  to the standard form with k significant points
// store the result in string t[]
void TransformNum(char s[],int k,char t[])
{	
	int count = CountBeforePoint(s);
	int u = 2;
	t[0] = '0';
	t[1] = '.';

	if(IsZero(s)) // s='0'
	{
		for(int i=0;i<k;i++)
			t[u++] = '0';
	}
	else if(IsLessThanOne(s)) // handle 0.XXXXXXXXX
	{
		int j=0;
		for(int i=0;j<k && s[i]!='\0';i++)
		{	
			if(j>0)
			{
				t[u++] = s[i];
				j++;
			}
			else if(s[i]!='0' && s[i]!='.')
			{
				t[u++] = s[i];
				j++;
			}		
		}
		count = -count;
	}
	else if(s[0]!='0' && HasPoint(s) && k>count) // handle 1230.45 and k=5
	{
		k++;
		for(int i=0;i<k && s[i]!='\0';i++)
		{
			if(s[i]!='.')
				t[u++] = s[i];
		}
	}
	else
	{
		for(int i=0;i<k && s[i]!='\0';i++)
		{
			if(s[i]!='.')
				t[u++] = s[i];
		}
	}
	sprintf(t+u,"*10^%d",count);
}

int main()
{
	int N;
	char s1[MAXSIZE],s2[MAXSIZE];
	char t1[MAXSIZE],t2[MAXSIZE];
	char r1[MAXSIZE],r2[MAXSIZE];

	scanf("%d %s %s",&N,s1,s2);

	// first delete the leading zeros before the number
	ChopLeadingZeros(s1,t1);
	ChopLeadingZeros(s2,t2);

	// transform the num to the required format
	TransformNum(t1,N,r1);
	TransformNum(t2,N,r2);

	// compare the transformed result
	if(strcmp(r1,r2)==0)
		printf("YES %s",r1);
	else
		printf("NO %s %s",r1,r2);
	
	return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值