PAT (Advanced Level) 1060 Are They Equal

Posted vividbingo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1060 Are They Equal相关的知识,希望对你有一定的参考价值。

题解

  找出有效的字符串(t),第一个非零数字的位置(zero)和小数点的位置(point)。

    若 s = "0012.104654",N = 4 则 t = "12104654",zero = 2,point = 4,所以答案为 0.1210*10^(point-zero)

    若 s = "0000.00010465",N = 4 则 t = "10465",zero = 8,point = 4,所以答案为 0.1046*10^(point-zero+1)

    注意 s = "0.0000000"的情况,若 N = 3,则答案为0.000*10^0

代码

#include<bits/stdc++.h>
using namespace std;
string process(int N,string s,int & k);
int main()
{
    int i,N,k1,k2;
    string str1,str2;
    cin>>N>>str1>>str2;
    
    str1=process(N,str1,k1);
    str2=process(N,str2,k2);
    
    if(str1==str2 && k1==k2)    printf("YES %s*10^%d",str1.c_str(),k1);
    else    printf("NO %s*10^%d %s*10^%d",str1.c_str(),k1,str2.c_str(),k2);
    
    system("pause");
    return 0;
}
string process(int N,string s,int & k)
{
    int i,zero,point;
    string t;
    zero=point=-1;
    for(i=0;i<s.size();i++)
    {
        if(s[i]==.)   point=i;
        else if(s[i]==0 && zero==-1)  continue;
        else 
        {
            if(zero==-1)    zero=i;
            t+=s[i];
        }
    }

    if(zero==-1) 
    {
        t.insert(0,N,0);
        k=0;
        return "0."+t;
    }
    else
    {
        if(point==-1) point=i;
        if(t.size()<N) t.insert(t.size(),t.size()-N,0);
        if(zero>point)  k=point-zero+1;
        else    k=point-zero;
        return "0."+t.substr(0,N);
    }   
}

以上是关于PAT (Advanced Level) 1060 Are They Equal的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced 1060 Are They Equal (25分)

PTA(Advanced Level)1060.Are They Equal

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079