【HDU】2054 A == B ?
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054
题目:
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
分析:
题目说明是比较A B是否相同 ,但却没说明A、B是否为小数或者范围大小等。这道题需要用数组来存。此外也不能存到数组后,直接对字符串数组进行比较,因为有可能是1.00和1。所以需要对获取到的a、b进行化简,然后再字符串比较。
此外,设置a、b数组的大小,要尽可能大些,之前设小了,超时。后来把数组大小从1000到10000,最终改为100005,才ac的。
还有,才发现自己曾经的一个问题,自己之前忽略了strlen()在获取输入内容后,末尾带有'\0';所以在循环输入的题,考虑到不被前一数组的值影响,都靠把数组内容重新初始化为' '等,再输入内容,求长度。搞麻烦了很多Orz 实际上直接使用strlen即可。
代码:
#include <stdio.h>
#include <string.h>
char a[100005],b[100005];
void Simplify(char *a)
{
int len=strlen(a);
int i,ok=0;
for (i=0; i<len; i++)//找到小数点位置,也可以用strstr(a,".")
{
if (a[i] == '.'){ ok=1; break;}
}
//printf("%d\n",strstr(a,".")-a);
if (ok)
{
for (i=len-1; i>=0; i--)//化简处理
{
if (a[i] == '0')a[i]='\0';
else break;
}//从后往前看。一直是0,则去掉0 继续循环,直到遇到非零,跳出循环
if (a[i] == '.') a[i]='\0';//(小数点后全为0,则)去掉小数点
}
}
int main()
{
while (scanf("%s %s",&a,&b) != EOF)
{
Simplify(a);
Simplify(b);
if (strcmp(a,b)) printf("NO\n");
else printf("YES\n");
}
return 0;
}