A == B ? HDU - 2054

A == B ?     HDU - 2054

https://vjudge.net/problem/HDU-2054
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

题意

判断A,B两个数字是否相同

思路

找小数点,从小数点两边同时对比,如果一个数组到端点了就直接判断另一个数组剩余的是否为0
没有小数点想象最后有一个小数点

代码
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
    char f[14000], g[14000];
    while(~scanf("%s %s", f, g)){
        int lenf=strlen(f),leng=strlen(g);
        int nodef=-1,nodeg=-1;
        for(int i=0;i<lenf;i++){//找小数点
            if(f[i]=='.'){
                nodef=i;
                break;
            }
        }
        for(int i=0;i<leng;i++){
            if(g[i]=='.'){
                nodeg=i;
                break;
            }
        }
        int ef=nodef+1,eg=nodeg+1;//e_是指向小数部分
        int bf=nodef-1,bg=nodeg-1;//b_是指向整数部分
        if(nodef==-1) ef=lenf,bf=lenf-1;
        if(nodeg==-1) eg=leng,bg=leng-1;
        int a=0;
        while(1){//开始遍历
            if(ef<lenf&&eg<leng&&f[ef++]!=g[eg++]){//没有到端点,就一直判断是否相等
                printf("NO\n");
                a=1;
                break;
            }
            if(ef==lenf&&eg<leng&&g[eg++]!='0'){//其中一个到了端点,判断另一个是否为'0'
                printf("NO\n");
                a=1;
                break;
            }
            if(eg==leng&&ef<lenf&&f[ef++]!='0'){
                printf("NO\n");
                a=1;
                break;
            }
            if(bf>-1&&bg>-1&&f[bf--]!=g[bg--]){
                printf("NO\n");
                a=1;
                break;
            }
            if(bf==-1&&bg>=0&&g[bg--]!='0'){
                printf("NO\n");
                a=1;
                break;
            }
            if(bg==-1&&bf>=0&&f[bf--]!='0'){
                printf("NO\n");
                a=1;
                break;
            }
            if((ef>=lenf&&eg>=leng)&&(bf<=-1&&bg<=-1))//最后都到了端点,跳出循环
            break;
        }
        if(a==0)
        printf("YES\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44410512/article/details/86745227