【HDOJ】2054 A == B ?
这道题目起初看,so easy。再看一下ac率,注意到没有说明变量类型。显然是一道字符串的题。需要考虑+/-符号位,+.1、-.1、00010.0、+0、-00.00等情况,同时数组开到100000以上。wa了两次,第二次是才发现输出的是YES/NO,我一直输出的是yes/no,测了很多数据都是对的才发现是大小写错误,巨坑,而且代码写的也挺乱。
#include <stdio.h> #define MAXNUM 110000 char a[MAXNUM]; char b[MAXNUM]; void chg(char a[]) { int i, beg, end, tmp; tmp = strlen(a); if (a[0] != '+' && a[0] != '-') { for (i=tmp; i>=0; --i) a[i+1] = a[i]; a[0] = '+'; tmp++; } beg = 1; while (beg<tmp && a[beg] == '0') beg++; if (beg) { if (beg>1 && a[beg] == '.') beg--; if (beg == tmp) beg--; for(i=beg; i<=tmp; ++i) a[i-beg+1] = a[i]; } tmp = 0; for (i=0; i<strlen(a); ++i) if (a[i] == '.') { tmp = i; break; } if (tmp == 0) { tmp = strlen(a); if (tmp==2 && a[1]=='0') a[0] = '+'; return ; } else if (tmp == 1) { end = strlen(a); for (i=end; i>=tmp; --i) a[i+1] = a[i]; a[tmp] = '0'; tmp++; } end = strlen(a) - 1; while (end>tmp && a[end] == '0') end--; if ( end == tmp ) a[end] = '\0'; else a[end+1] = '\0'; tmp = strlen(a); if (tmp == 2 && a[1] == '0') a[0] = '+'; } int main() { int len1, len2; int i, flg; while (scanf("%s %s", a, b) != EOF) { chg(a); chg(b); len1 = strlen(a); len2 = strlen(b); //printf("%s\n", a); //printf("%s\n", b); if (len1 != len2) printf("NO\n"); else { flg = 1; for (i=0; i<len1; ++i) if (a[i] != b[i]) { flg = 0; break; } if (flg) printf("YES\n"); else printf("NO\n"); } } return 0; }