HDUOJ2054:A == B ?

版权声明:qq836678589 https://blog.csdn.net/weixin_43924623/article/details/90770081

A == B ?

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

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


import java.math.BigDecimal;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) throws Exception{
        Scanner a = new Scanner(System.in);
        while(a.hasNext()){
            BigDecimal b = a.nextBigDecimal();
            BigDecimal c = a.nextBigDecimal();
            if(b.compareTo(c)==0){
                System.out.println("YES");
            }else{
                System.out.println("NO");
            }
        }
    }
    
}

总结: 由于计算机用二进制表示,小数尾数的处理上无法精确。java的BigDecimal可以比较好的处理这种情况。
学习了三个方法:
hasNest()
BigDecimal()
b.comapreTo©==0

猜你喜欢

转载自blog.csdn.net/weixin_43924623/article/details/90770081