题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054
A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 66431 Accepted Submission(s): 10353
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
比较简单,我直接上JAVA的代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
public static void main(String args[]){
Scanner cin=new Scanner(new BufferedInputStream(System.in));
BigDecimal a,b;
int n;
while(cin.hasNext()){
a=cin.nextBigDecimal();b=cin.nextBigDecimal();
String sa=a.stripTrailingZeros().toPlainString();
String sb=b.stripTrailingZeros().toPlainString();
if(sa.compareTo(sb)==0) System.out.println("YES");
else System.out.println("NO");
}
}
}
C++版,最后还是发现C++的string好用。
1.考虑正负号
2.去除前导零
3.去除后道零
4.一位位比对
算法结束
#include<iostream>
using namespace std;
int main()
{
string a,b;
while(cin>>a>>b)
{
bool fa=1,fb=1;
if(a[0]!='-') fa=0;//判断a是否非负
if(b[0]!='-') fb=0;
if(fa!=fb)
{
cout<<"NO"<<endl;
continue;
}
if(a[0]=='+'||a[0]=='-') a=a.substr(1);//如果有符号则去掉符号
if(b[0]=='+'||b[0]=='-') b=b.substr(1);
int la=0,lb=0,ra=a.size(),rb=b.size(),pa=1<<30,pb=1<<30;
for(int i=0;i<ra;i++)//找出a中'.'的位置
if(a[i]=='.')
{
pa=i;
break;
}
while(a[la]=='0'&&la+1<pa&&la+1<ra) la++;
while((a[--ra]=='0'||a[ra]=='.')&&ra>=pa) ;
for(int i=0;i<rb;i++)
if(b[i]=='.')
{
pb=i;
break;
}
while(b[lb]=='0'&&lb+1<pb&&lb+1<rb) lb++;
while((b[--rb]=='0'||b[rb]=='.')&&rb>=pb) ;
a=a.substr(la,ra-la+1);//截取有效部分
b=b.substr(lb,rb-lb+1);
if(a!=b) cout<<"NO"<<endl;
else cout<<"YES"<<endl;;
}
return 0;
}