PAT (Advanced Level) 1060. Are They Equal (25)

模拟题。坑点较多。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

int n;
string s,t;
struct X
{
    string f;
    int idx;
} ans[5];

string change(string a)
{
    string res;
    if(a.length()-2<n)
    {
        for(int i=a.length()-2;i<n;i++) a=a+"0";
        res=a;
    }

    else if(a.length()-2>n)
    {
        res="0.";
        for(int i=2;i<n+2;i++) res=res+a[i];
    }
    else res=a;
    return res;
}

string qu(string a)
{
    string res;
    if(a.length()==1&&a[0]==‘0‘) return a;

    int pos=-1;
    for(int i=0; i<a.length(); i++) if(a[i]==‘.‘) pos=i;

    if(pos==-1)
    {
        for(int i=0; i<a.length(); i++)
        {
            if(a[i]==‘0‘) continue;
            else
            {
                for(int j=i; j<a.length(); j++) res=res+a[j];
                break;
            }
        }
    }
    else
    {
        for(int i=0; i<pos-1; i++)
        {
            if(a[i]==‘0‘) continue;
            else
            {
                for(int j=i; j<pos-1; j++) res=res+a[j];
                break;
            }
        }
        for(int i=pos-1; i<a.length(); i++) res=res+a[i];
    }

    if(res.length()==0) res="0";
    return res;
}

void R(int a)
{
    bool ling=1;
    for(int i=0; i<ans[a].f.length(); i++)
    {
        if(ans[a].f[i]==‘.‘) continue;
        if(ans[a].f[i]!=‘0‘) ling=0;
    }

    if(ling) ans[a].idx=0;
}

void PUT(int a)
{
    cout<<ans[a].f<<"*10^"<<ans[a].idx;
}

int main()
{
    scanf("%d",&n);
    cin>>s>>t;

    s=qu(s);
    t=qu(t);

    if(s[0]==‘0‘&&s[1]==‘.‘)
    {
        int num=0;
        for(int i=2; i<s.length(); i++)
        {
            if(s[i]==‘0‘) num++;
            else
            {
                ans[0].idx=-num;
                ans[0].f="0.";
                for(int j=i; j<s.length(); j++) ans[0].f=ans[0].f+s[j];
                break;
            }
        }
        if(ans[0].f.length()==0)
        {
            ans[0].f="0.0";
            ans[0].idx=0;
        }
    }

    else
    {
        int pos=-1;
        for(int i=0; i<s.length(); i++) if(s[i]==‘.‘) pos=i;
        if(pos!=-1)
        {
            ans[0].f="0.";
            for(int i=0; i<s.length(); i++) if(s[i]!=‘.‘) ans[0].f=ans[0].f+s[i];
            ans[0].idx=pos;
        }
        else
        {
            ans[0].f="0."+s;
            ans[0].idx=s.length();
        }
    }

    ans[0].f=change(ans[0].f);

    if(t[0]==‘0‘&&t[1]==‘.‘)
    {
        int num=0;
        for(int i=2; i<t.length(); i++)
        {
            if(t[i]==‘0‘) num++;
            else
            {
                ans[1].idx=-num;
                ans[1].f="0.";
                for(int j=i; j<t.length(); j++) ans[1].f=ans[1].f+t[j];
                break;
            }
        }
        if(ans[1].f.length()==0)
        {
            ans[1].f="0.0";
            ans[1].idx=0;
        }
    }
    else
    {
        int pos=-1;
        for(int i=0; i<t.length(); i++) if(t[i]==‘.‘) pos=i;
        if(pos!=-1)
        {
            ans[1].f="0.";
            for(int i=0; i<t.length(); i++) if(t[i]!=‘.‘) ans[1].f=ans[1].f+t[i];
            ans[1].idx=pos;
        }
        else
        {
            ans[1].f="0."+t;
            ans[1].idx=t.length();
        }
    }

    ans[1].f=change(ans[1].f);

    R(0); R(1);

    if(ans[0].f==ans[1].f&&ans[0].idx==ans[1].idx)
    {
        printf("YES ");
        PUT(0);
    }

    else
    {
        printf("NO ");
        PUT(0);
        cout<<" ";
        PUT(1);
        cout<<endl;
    }

    return 0;
}
时间: 2024-10-03 23:16:13

PAT (Advanced Level) 1060. Are They Equal (25)的相关文章

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

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: 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 flo

PAT Advanced Level 1053 Path of Equal Weight

1053 Path of Equal Weight (30)(30 分) Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

PAT (Advanced Level) 1089. Insert or Merge (25)

简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[maxn],

PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. #include<iostream> #include<cstring> #include<cmath> #include<al

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&

PAT (Advanced Level) 1009. Product of Polynomials (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; double a[3000],b[3000],c[3000]; int k; int main() { for(int i=0;i<=2000;i++) a[i]=b[i]=c[i]=0; int Max=-1