A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:给你两个数,判断是否相等。
刚看到这个题时,觉得这也太简单了,两个数相减,判断差是否是0不就行了。于是很快写好代码,果断提交,Wrong Answer!顿时无语。后来听人说要用字符串,又写了一个代码还是WrongAnswer。原来是给的数可能是小数且可能在末尾还有无用的0,要把这些0去掉再比较。查了一些资料以后,终于把这个题搞定了,和大家分享一下我的代码:
#include<stdio.h> #include<string.h> char *change(char *s) { int len=strlen(s); if(strchr(s,'.')!=NULL) /*如果有小数点*/ { while(s[--len]=='0'); //去掉小数点后面的没用的0,从后往前去 if(s[len]=='.') /*小数点后全是0时执行此操作*/ len--; /*把小数点去掉*/ s[len+1]='\0'; /*保留有效的数*/ } return s; } int main() { char s1[14000],s2[14000]; while(~scanf("%s %s",s1,s2)) { if(!strcmp(change(s1),change(s2))) /*字符串比较,相等返回0*/ printf("YES\n"); else printf("NO\n"); } return 0; }注意:这个题不用考虑前导0,即008这种情况不用考虑。
strchr(s,'.')函数是判断在字符串s中是否含有小数点,如果没有就返回空指针;有的话就返回指针位置,即地址。