HDU 2054 A == B ?

A == B ?

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


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. 全部为0的情况

            2.有正负号的情况

            3.浮点数(删掉小数点前面的0和小数点后面的0)

例子:       

            1:  +00.0和-0 、+0和-0 、00.00 和0  等等全部为0 的情况;
            2:  001.1和1.10 、000.01 和0.010 、+000.01 和-0.010  等等

            3:  01.10 和01.1  、+01.10 和-01.1等等

            4:  001.1 和1.1、-001.1 和+1.1  等等

import java.util.*;
import java.io.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(new BufferedInputStream(System.in));
		while(sc.hasNext()){
			String s1=sc.next();
			String s2=sc.next();
			s1=fun(s1);
			s2=fun(s2);
			if(s1.equals(s2))
				System.out.println("YES");
			else System.out.println("NO");
		}
	}
	public static String fun(String s){
		boolean b=true;
		//处理全部为0的情况
		for(int i=0;i<s.length();i++){
			if(s.charAt(i)!='0'&&s.charAt(i)!='.'&&s.charAt(i)!='+'&&s.charAt(i)!='-'){
				b=false;
				break;
			}
		}
		if(b==true) return "0";
		//处理 正负号的情况
		StringBuffer bu=new StringBuffer(s);
		if(s.charAt(0)!='+'&&s.charAt(0)!='-'){
			
			for(int i=0;i<bu.length();i++){
				if(bu.charAt(i)=='0'){
					bu.replace(i, i+1, "");
					i--;
				}
				else break;
			}
		}
		else{
			for(int i=1;i<bu.length();i++){
				if(bu.charAt(i)=='0'){
					bu.replace(i, i+1, "");
					i--;
				}
				else break;
			}
		}
		//处理浮点数(删掉小数点前面的0和小数点后面的0)
		if(s.contains(".")){
			bu.reverse();
			for(int i=0;i<bu.length();i++){
				if(bu.charAt(i)=='0'){
					bu.replace(i, i+1, "");
					i--;
				}
				else break;
			}
			int m=bu.indexOf(".");
			bu.replace(m, m+1, "");
			bu.reverse();
		}
		return bu.toString();
	}
}


 

原文地址:https://www.cnblogs.com/jiangu66/p/3231013.html