杭电oj2054A==B 超时问题

版权声明: https://blog.csdn.net/qq_40829288/article/details/80641684


原文链接:原文地址

A == B ?

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 123229    Accepted Submission(s): 19702


Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
 

Input
each test case contains two numbers A and B.
 

Output
for each case, if A is equal to B, you should print "YES", or print "NO".
 

Sample Input
 
  
1 2
2 2
3 3
4 3
 

Sample Output
 
  
NO
YES
YES
NO
用while(scanf("%s%s",a,b))会超出时间限制;用while(scanf("%s%s",a,b)!=EOF)或while(cin>>a>>b)或while(~scanf("%s%s",a,b))就不会超出;
#include<iostream>
#include<cstring>
using namespace std;
char a[100000]={0},b[100000]={0};
int i,j,k,s1,s2;
int main()
{
    /*
    用while(scanf("%s%s",a,b))会超出时间限制;
    用while(scanf("%s%s",a,b)!=EOF)或while(cin>>a>>b)或while(~scanf("%s%s",a,b))就不会超出;
    */
    while(~scanf("%s%s",a,b))
    {
        s1=strlen(a);
        s2=strlen(b);
        if(strchr(a,'.'))
        {
            for(i=s1-1;a[i]=='0';i--)
            {

                s1--;
                a[i]='\0';
            }
            if(a[i]=='.')
            {
                a[i]='\0';
                s1--;
            }
        }
        k=0;
        if(a[0]=='-')
            {
                j=1;
            }
        else
            {
                j=0;
            }
        for(i=j;i<s1;i++)
           {
               if(a[i]=='0')
                    k++;
               else
                    break;
           }
        s1-=k;
        for(i=j;i<s1;i++)
            {
                a[i]=a[i+k];
            }
        if(strchr(b,'.'))
        {
            for(i=s2-1;b[i]=='0';i--)
            {
                b[i]='\0';
                s2--;
            }
            if(b[i]=='.')
            {
                b[i]='\0';
                s2--;
            }
        }
        if(b[0]=='-')
            {
                j=1;
            }
        else
            {
                j=0;
            }
        k=0;
        for(i=j;i<s2;i++)
            {
                if(b[i]=='0')
                    k++;
                else
                    break;
            }
        s2-=k;
        for(i=j;i<s2;i++)
            {
                b[i]=b[i+k];
            }
        if(s1==s2)
        {
           for(i=0;i<s1;i++)
                if(a[i]!=b[i])
                    break;
            if(i==s1)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40829288/article/details/80641684
今日推荐