A == B?

题意:给你两个数,让你判断两个是否相等?

思路:这道题很容易把它想简单,就是他会有很多干扰的样例,例如前导零,后导零等,所以要去掉这一些情况的干扰!首先去掉前导零,其实无需去掉,只需忽略掉他们即可,后导零,从后往前面读,把是零的位用 “\0” 覆盖掉即可,去掉这些干扰因素之后,直接字符串比较就可以了!这里有一个小细节,就是诸如 0.000000000000 这一类的情况,及去掉最后一个后导零为的下一位是小数点的话,直接可以将小数点去掉!

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <queue>
#define N 10000000

using namespace std;

char s[N];
char p[N];

void solve( char *s )
{
    char *t = s + strlen( s ) - 1;
    if( strchr( s, '.' ) )
    {
        while( *t == '0' )
        {
            *t-- = 0;
        }
        //cout << "w";
    }
    if( *t == '.' )
    {
        *t = 0;
    }
}

int main()
{
    while( scanf( "%s%s", s, p ) != EOF )
    {
        if( ( s[0] == '+' && p[0] == '-' ) || ( p[0] == '+' && s[0] == '-' ) )
        {
            cout << "NO" << endl;
            continue;
        }
        char *ss = s;
        char *pp = p;
        while( *ss == '0' || *ss == '+' || *ss == '-' )
        {
            //cout << "w";
            ss++;
        }
        while( *pp == '0' || *pp == '+' || *pp == '-' )
        {
            //cout << "w";
            pp++;
        }
        solve( ss );
        solve( pp );
        printf( "%s\n", strcmp( ss, pp ) == 0 ? "YES" : "NO" );
    }
}


猜你喜欢

转载自blog.csdn.net/u011564456/article/details/25661961