HDU

来源:互联网 发布:塔罗牌占卜软件 编辑:程序博客网 时间:2024/06/14 17:59

                                     A == B ?

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

思路:当初还以为是简单的比较相等(ps:看来我还是太天真了),这个题实际上是大数的比较相等。

注意:还带小数点,没有前导0的问题,但后导零是要注意的

(ps:题目中没说,全靠脑洞,我也是醉了,一个一个的试出来的

方法:把来个数字存到两个字符数组里,比之前处理下后导0即可。

代码如下:

#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int N=500000;char a[N];char b[N]; void hdzero(char *t)//后导零处理 {int i;int len=strlen(t);if(strstr(t,".")!=NULL)//strstr函数:查找 字符串(点 第一次出现在字符串t中的位置。如果没有返回NULL { for( i=len-1;t[i]=='0';i--)//从后往前一次消除后导0 t[i]='\0';if(t[i]=='.')//如果小数点后全是0,连小数点也去掉 t[i]='\0';}}int main(){while(~scanf("%s%s",a,b)){hdzero(a);hdzero(b);if(strcmp(a,b)==0)//比较处理完的数,相等就YES否则就NO cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;}
关于strstr的简单介绍;

strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。

如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

因为此题不需要考虑前导零,但要考虑小数点后面的0,所以用这个函数方便比较。