HDUOJ 2054 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
一开始我大意了啊,来骗,来偷袭。我直呼水题,可是哪有那么简单呢,缘来是需要不同格式相等的数也要正确比较,于是利用字符串进行比较,去掉后面多余的无效0。
主要用到字符串处理函数
strstr()用于检验目标字符串中第一次出现某个字符或者某段字符的位置
strcmp()用于字符串的比较,若字符串相等,则返回0
同时字符串数组题目没有声明大小,尽量开大点,这里开到5000,可以AC
#include<stdio.h>
#include<string.h>
char a[5000],b[5000];
void fomat(char a[]) {
int len = strlen(a);
//去除小数点最末位无效0
if(strstr(a,".")) {
for(int i = len - 1; a[i] == '0'; i--) {
a[i]='\0';
len--;
}
}
if(a[len-1] == '.') {
a[len-1]='\0';
}
}
int main() {
while(~scanf("%s %s",a,b)) {
fomat(a);
fomat(b);
if(!strcmp(a,b)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}