X - A == B ?(第二季水)

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
 
 
这个题神坑!!!
开始我写的代码考虑到数字前面多余的0和后面多余的0以及小数点
依旧是 结果错误!!!
最后发现不用考虑数字前面多余的0,出题的bug!!!   只需考虑小数点及其后末尾多余的0
以下为考虑全面的代码
#include<iostream>
#include<string.h>
using namespace std;
char a[20000],b[20000];
void f(char s[])
{
    int n;
    n=strlen(s);
    while(1){
        if(s[0]=='0'){
            for(int j=0;j<n;j++){
                s[j]=s[j+1];
            }
            n--;
        }
        else break;
    }
    if(strchr(s,'.')){
        for(int i=n-1;i>=0;i--){
            if(s[i]=='0'){
                s[i]='\0';
                n--;
            }
            else break;
        }
    }
    if(s[n-1]=='.')s[n-1]='\0';
}
int main()
{
    while(cin>>a>>b){
        f(a);
        f(b);
        if(strcmp(a,b))cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}


以下为AC代码

#include<iostream>
#include<string.h>
using namespace std;
char a[20000],b[20000];
void f(char s[])
{
    int n=strlen(s);
    if(strchr(s,'.')!=NULL){
        for(int i=n-1;i>=0;i--){
            if(s[i]=='0'){
                s[i]='\0';
                n--;
            }
            else break;
        }
    }
    if(s[n-1]=='.')s[n-1]='\0';
}
int main()
{
    while(cin>>a>>b){
        f(a);
        f(b); 
        if(strcmp(a,b))cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}

 

posted @ 2016-02-10 20:01  Not-Bad  阅读(234)  评论(0编辑  收藏  举报