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×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 Aand 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

 注意输入数据存在以下几种情况:

 1.120

 2.0.012

 3.00120

 4. 0 , 0.0 ,0.00 

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
typedef long long LL;
const int Max=205;
vector<char> x;
vector<char> y;
//求指数
int index(char *str)
{
    int i,Num,Len;
    //排除输入数据类似00100中的无效的0
    i=0;
    Len=strlen(str);
    while(str[i]=='0')
    {
        if(str[i+1]=='.')
           break;
        i+=1;
    }
    if(i==Len) return 0;
    Num=0;
    //若该数小于1
    if(str[i]=='0')
    {
        i+=2;
        while(str[i]=='0')
        {
            Num+=1;
            i+=1;
        }
        //输入数据类似 0.0,0.00的 要特别考虑
        if(i==Len)
            return 0;
        else
            return -Num;
    }
    //若该数大于1
    else
    {
        while(str[i]!='.'&&i<Len)
        {
            Num+=1;
            i+=1;
        }
        return Num;
    }
}
int main()
{
    int N,Len,i,Num,u,v;
    char A[Max],B[Max];
    scanf("%d %s %s",&N,A,B);

    u=index(A);
    v=index(B);

    Len=strlen(A);
    i=Num=0;
    //找到第一个有效数字的位置
    while(A[i]=='0'||A[i]=='.')
    {
        if(i==Len)
          break;
        i+=1;
    }
    while(Num<N)
    {
        if(i<Len)
        {
           if(A[i]!='.')
           {
              x.push_back(A[i]);
              Num+=1;
           }
           i+=1;
        }
        else
        {
            x.push_back('0');
            Num+=1;
        }
    }
    Len=strlen(B);
    i=Num=0;
    while(B[i]=='0'||B[i]=='.')
    {
        if(i==Len)
          break;
        i+=1;
    }
    while(Num<N)
    {
        if(i<Len)
        {
           if(B[i]!='.')
           {
              y.push_back(B[i]);
              Num+=1;
           }
           i+=1;
        }
        else
        {
            y.push_back('0');
            Num+=1;
        }
    }
    if(x==y&&u==v)
    {
        printf("YES 0.");
        Len=x.size();
        for(i=0;i<Len;i++)
        {
            printf("%c",x[i]);
            if(i==Len-1)
                printf("*10^%d",u);
        }
    }
    else
    {
        printf("NO 0.");
        Len=x.size();
        for(i=0;i<Len;i++)
        {
            printf("%c",x[i]);
            if(i==Len-1)
                printf("*10^%d 0.",u);
        }
        Len=y.size();
        for(i=0;i<Len;i++)
        {
            printf("%c",y[i]);
            if(i==Len-1)
               printf("*10^%d",v);
        }
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/ZCMU_2024/article/details/84994410
今日推荐