zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    关于求 p_i != i and p_i != i+1 的方案数的思考过程
    poj 3041 Asteroids 二分图最小覆盖点
    poj 1325 Machine Schedule 最小顶点覆盖
    poj 1011 Sticks 减枝搜索
    poj 1469 COURSES 最大匹配
    zoj 1516 Uncle Tom's Inherited Land 最大独立边集合(最大匹配)
    Path Cover (路径覆盖)
    hdu 3530 SubSequence TwoPoint单调队列维护最值
    zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
    poj 1466 Girls and Boys 二分图最大独立子集
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5551863.html
Copyright © 2011-2022 走看看