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(); } }