A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32921 Accepted Submission(s): 5033
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
题目大意:给你两个数,判断这两个数是否相等。看似简单的题,其实是一道模拟题,要对字符串进行处理的题,它给的数十很大很大的,double精度完全不够。所以看你如何操作了,代码有注释。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <cstring>
#include <string>
using namespace std;
char a[1000005], b[1000005];
bool fa, fb;
bool as, bs;
void a_sign() //判断+、-符号
{
int i, la;
la = strlen(a);
if(a[0] == '-')
{
as = false;
for(i = 0; i < la-1; i++)
{
a[i] = a[i+1];
}
a[la-1] = 0;
}
else if(a[0] == '+')
{
as = true;
for(i = 0; i < la-1; i++)
{
a[i] = a[i+1];
}
a[la-1] = 0;
}
else as = true;
}
void b_sign()
{
int i, lb;
lb = strlen(b);
if(b[0] == '-')
{
bs = false;
for(i = 0; i < lb-1; i++)
{
b[i] = b[i+1];
}
b[lb-1] = 0;
}
else if(b[0] == '+')
{
bs = true;
for(i = 0; i < lb-1; i++)
{
b[i] = b[i+1];
}
b[lb-1] = 0;
}
else bs = true;
}
void a_init()
{
int i, la;
la = strlen(a);
if(a[0] == '.') //如果第一个是'.',在他前面加'0'
{
for(i = la-1; i >= 0; i--)
{
a[i+1] = a[i];
}
a[0] = '0';
a[la+1] = 0;
}
la = strlen(a);
if(a[la-1] == '.') //如果最后一个是'.',在他后面加'0'
{
a[la] = '0';
a[la+1] = 0;
}
//cout << "a = " << a << endl;
}
void b_init()
{
int i, lb;
lb = strlen(b);
if(b[0] == '.')
{
for(i = lb-1; i >= 0; i--)
{
b[i+1] = b[i];
}
b[0] = '0';
b[lb+1] = 0;
}
lb = strlen(b);
if(b[lb-1] == '.')
{
b[lb] = '0';
b[lb+1] = 0;
}
//cout << "b = " << b << endl;
}
void a_before_zero() //把前置的0去掉
{
int i, la, pos, cnt;
i = 0;
while(a[i] == '0' && (a[i+1] >= '0' && a[i+1] <= '9'))
{
i++;
}
pos = i;
cnt = 0;
la = strlen(a);
for(i = pos; i < la; i++)
{
a[cnt++] = a[i];
if(a[i] == '.') fa = true;
}
a[cnt] = 0;
//cout << "a = " << a << endl;
}
void b_before_zero()
{
int i, lb, pos, cnt;
i = 0;
while(b[i] == '0' && (b[i+1] >= '0' && b[i+1] <= '9'))
{
i++;
}
pos = i;
cnt = 0;
lb = strlen(b);
for(i = pos; i < lb; i++)
{
b[cnt++] = b[i];
if(b[i] == '.') fb = true;
}
b[cnt] = 0;
//cout << "b = " << b << endl;
}
void a_back_zero() //如果是浮点数,把后置的0去掉
{
if(!fa) return;
int i, la;
la = strlen(a);
i = la - 1;
while(i > 1 && a[i] == '0')
{
if(a[i-1] != '.')
{
a[i] = 0;
i--;
}
else
{
a[i] = 0;
i--;
a[i] = 0;
i--;
break;
}
}
//cout << "a = " << a << endl;
}
void b_back_zero()
{
if(!fb) return;
int i, lb;
lb = strlen(b);
i = lb - 1;
while(i > 1 && b[i] == '0')
{
if(b[i-1] != '.')
{
b[i] = 0;
i--;
}
else
{
b[i] = 0;
i--;
b[i] = 0;
i--;
break;
}
}
//cout << "b = " << b << endl;
}
int main()
{
while(scanf("%s %s", a, b) != EOF)
{
fa = fb = false;
as = bs = false;
a_sign();
b_sign();
a_init();
b_init();
a_before_zero();
b_before_zero();
a_back_zero();
b_back_zero();
if(a[0] == '0' && a[1] == 0 && b[0] == '0' && b[1] == 0) //+0和-0是相等的
{
printf("YES\n");
}
else if(as == bs && strcmp(a, b) == 0) //直接字符串处理
{
printf("YES\n");
}
else printf("NO\n");
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
}
return 0;
}