HDU 2054 A == B ?

       A == B ?

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

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

【思路分析】

一看就是超级水题,可是坑却特别深。。。。。。

应该注意以下几点细节:

1 题中没有说明两个数的类型,所以可能会出现连__int64/long long都存不下的数,因此需要字符串来存储数据(大数),或者使用java的大数秒A;

2 注意前导零以及后导零 即 000001 和1相等,1.000000和1相等,因此需要将多余的零以及小数点给去掉;

3 注意-0 和 0 是相等的  所以数据中只有0、.、-、+时两数是相等的

代码如下:

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <string>

#include <algorithm>

#include <cstring>

#include <cmath>

using namespace std;

void change_front(string &str)

{

    int p1,p2;

    p1=p2=0;

    if (str[0] == '-')

    {

        p1++;

        p2=p1;

    }

    while (str[p1] == '0')

        p1++;

    str.erase(p2,p1-p2);

}

void change_back(string &str)

{

    int l=str.length()-1;

    while (str[l] == '0')//排除后导零

        l--;

    while (str[l] == '.')//排除小数点

        l--;

    str.erase(l+1);//删去从l指针后的零、小数点

}

int if_zero(string &str1,string &str2)

{

    int l1=str1.length()-1;

    int l2=str2.length()-1;

    int x=1;

    int y=1;

    for (int i=0;i<=l1;i++)

        if (str1[i] != '0' && str1[i] != '.' && str1[i] != '+' && str1[i] != '-')

    {

        x=0;

        break;

    }

    for (int i=0;i<=l2;i++)

        if (str2[i] != '0' && str2[i] != '.' && str2[i] != '+' && str2[i] != '-')

    {

        y=0;

        break;

    }

    if (x && y)

       x=1;

    return x;

 

}

int main()

{

    string a,b;

    int la,lb;

    while (cin>>a>>b)

    {

        if (if_zero(a,b))

        {

            cout<<"YES"<<endl;

            continue;

        }

        change_front(a);

        change_front(b);

        la=a.length()-1;

        lb=b.length()-1;

        for (int i=la;i>=0;i--)

            if (a[i] == '.')//若为小数,则进行后导零以及小数点的处理

        {

            change_back(a);

            break;

        }

        for (int i=lb;i>=0;i--)

            if (b[i] == '.')

        {

            change_back(b);

            break;

        }

        if (a == b)

            cout<<"YES"<<endl;

        else

            cout<<"NO"<<endl;

    }
    return 0;

}


 

 

另外搜了一个java的代码 看完之后默默地流泪@-@

import java.math.BigDecimal;

import java.util.*;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

BigDecimal a,b;

while(sc.hasNext()){

a=sc.nextBigDecimal();

    b=sc.nextBigDecimal();

    if(a.compareTo(b)==0)

        System.out.println("YES");

    else

        System.out.println("NO");    

}

}

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值