(C语言)HDU 2054 A == B ?

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 Input1 2
2 2
3 3
4 3
Sa
mple OutputNO
YES
YES
NO
题目链接

这个题目有坑,可能会不假思索地用两个常规整形变量去做,但是题目没说数值的大小,故用最好用字符串来记录,但在字符串中2.00和2.0是不一样的,002和2是不一样,所以比较时要从第一个不为0的字符开始,比较前要把.和后面的多余的零去掉;

#include<stdio.h>
#include<string.h>
void change(char *num){
 int len = strlen(num);
    char *p = num + len - 1;	
    if (strchr(num, '.'))	//判断是否是小数
     while (*p == '0'){	//去掉小数末尾多余的0
      *p = '\0';
      p--;
  }
    if (*p == '.') *p = '\0';	//如果是2.这种情况,便去除小数点
}
int main(void)
{
 char *pa,*pb;
 char a[100024],b[100024];
 while(~scanf("%s%s",&a,&b))
 {
  pa = a;pb = b;
  
  while(*pa==0) pa++;	//保证比较时要从第一个不为0的字符开始
  while(*pb==0) pb++;	//保证比较时要从第一个不为0的字符开始
  
  change(pa);change(pb);
  if(strcmp(pa, pb)!=0) printf("NO\n");
  else printf("YES\n");
 }
 return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43724031/article/details/86579871