【PAT】1060. 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*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.

翻译:如果一个机器只能记录3位有效数字,那么浮点数12300和12358.9就会被认为一样因为他们都会被用科学计数法保存为0.123*10^5。现在给你一个机器的有效位数和两个浮点数,你需要说出它们在该机器上是否会被认为相同。

INPUT FORMAT

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.

翻译:每个输入文件包含一组测试数据。每组测试数据包含3个数字N,A和B,N (<100)为有效位数的个数,A和B分别为需要比较的两个浮点数。每个浮点数都是非负的且不超过10^100,并且它的总位数小于100。

OUTPUT FORMAT

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.

翻译:对于每组输入数据,输出一行“YES”如果两个数字被认为一样,并且按照标准格式”0.d1…dN*10^k” (d1>0 除非数字为0)输出;否则输出“NO”,并且输出两个数字的标准格式。所有部分之间必须用空格隔开,并且末尾不能有多余空格。


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


解题思路

这道题的注意事项很多,基本思想是判断科学计数法的位数,然后比较本体是否相同。不过要注意的是科学计数法的位数判断中,如果整数部分为0,小数部分的前导0会变成负数位数,如果小数也为0,则位数又要改为0。本体的计数就在于第一个非0数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
char s[2][110];
int t[2][110],l[2];//整理后的float值,l[i]为本体位数
void print(int temp[]){
    printf("0.");
    for(int i=1;i<=N;i++){
        printf("%d",temp[i]);
    }
    printf("*10^%d",temp[0]);
}
int main(){
    scanf("%d%s%s",&N,&s[0],&s[1]);
    for(int i=0;i<2;i++){
        int tl=strlen(s[i]);
        int flag=0;
        for(int j=0;j<tl;j++){
            if(s[i][j]=='.'){
                flag=2;
                continue;
            }
            if(flag==0&&s[i][j]!='0')flag=1;
            if(flag==1)t[i][0]++;
            if(flag){
                if(flag==2&&t[i][0]<=0&&s[i][j]=='0')t[i][0]--;//如果整数位数为0,则为负指数 
                else{
                    if(flag==2&&s[i][j]!='0')flag=3;
                    t[i][++l[i]]=s[i][j]-'0';
                }
            }
        }
        if(flag==2)t[i][0]=0;
    }
    int flag=0;
    for(int i=0;i<=N;i++){
        if(t[0][i]!=t[1][i]){
          flag=1;
          break;
        }
    }
    if(flag==1){
        printf("NO ");
        print(t[0]);printf(" ");print(t[1]);printf("\n");
    }
    else{
        printf("YES ");
        print(t[0]);printf("\n");
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值