A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 84572 Accepted Submission(s): 13315
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
第一次看别人的代码AC的,这次自己用C++string类函数AC的,此题虽然不考虑正负号,但是代码显然还是有正负号的缺陷,加入正负号感觉更加麻烦,而且对结果会有副作用,因此先不考虑正负号了。
代码:
#include<iostream> #include<string> using namespace std; string qianzero(string a)//前导0检查 { if(a.find_first_not_of('0')!=string::npos) { a.erase(0,a.find_first_not_of('0'));//去除前导0,并且连小数点一并去除了(小数点也属于非零) } return a; } string houzero(string a)//后导0检查 { int l=-1,i; if(a.find('.')!=string::npos)//若有小数点则继续判断 { for(i=a.size()-1;i>=0;i--) { if(i==a.find('.'))//若非零数字超过了小数点,则只能截取到【小数点~最后一位】为止 { l=i-1; break; } if(a[i]!='0'&&a[i]!='.')//否则截取到【非零数字~最后】一位 { l=i; break; } } if(l==-1)//若根本找不到小数点,则直接返回(即不可以动后面的0) { return a; } a.erase(l+1,a.size()-1);//去掉后面的0 } return a; } string point(string a)//检查并将实际为0的各种类型归一为“0” { int i; for (i=0; i<a.size(); i++) { if(a[i]!='0'&&a[i]!='.')//若发现某个字符不是0且不是小数点,则此数一定不等于0,返回 return a; } return a="0";//否则干脆变成“0” } int main(void) { string a,b; while(cin>>a>>b) { a=qianzero(a); b=qianzero(b); a=houzero(a); b=houzero(b); a=point(a); b=point(b); //cout<<a<<' '<<b<<' '<<endl; if(a==b) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }