hdu 2054 A==B?(模擬問題)


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

 
           あなたに2つの数をあげて、この2つの数が等しいかどうかを判断します.简単な问题のように见えて、実は1本のシミュレーションの问题で、文字列に対して処理する问题、それは数十とても大きくて、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
"); } else if(as == bs && strcmp(a, b) == 0) // { printf("YES
"); } else printf("NO
"); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); } return 0; }