首页 > 代码库 > HDU-1753

HDU-1753

/********************************************************************@file     Main_practise.cpp@date     2014-8-28@author   Tiger@brief    大明A+B********************************************************************/#include <cstdio>#include <string>#include <cstring>#include <iostream>const int SIZE = 500;void Reverse(std::string& str);std::string Add(const std::string& strA, const std::string& strB, bool& Flag, bool b);int main(int argc, const char* argv[]){    //freopen("out.txt", "w", stdout);    std::string strA, strB;    while (std::cin >> strA >> strB)    {        std::string strA_A, strA_B;        if (strA.find(".") != std::string::npos)        {            strA_A = strA.substr(0, strA.find("."));            strA_B = strA.substr(strA.find(".")+1, strA.size()-strA.find(".")-1);        }        else        {            strA_A = strA;        }        std::string strB_A, strB_B;        if (strB.find(".") != std::string::npos)        {            strB_A = strB.substr(0, strB.find("."));            strB_B = strB.substr(strB.find(".")+1, strB.size()-strB.find(".")-1);        }        else        {            strB_A = strB;        }        bool bFlag = false;        std::string strSum_B = Add(strA_B, strB_B, bFlag, true);        Reverse(strA_A);        Reverse(strB_A);        if (bFlag)        {            ++strA_A.at(0);        }        std::string strSum_A = Add(strA_A, strB_A, bFlag, false);        Reverse(strSum_A);        if (!strSum_A.empty())        {            printf("%s", strSum_A.c_str());        }        if (!strSum_B.empty())        {            printf(".%s", strSum_B.c_str());        }        if (strSum_A.empty() && strSum_B.empty())        {            printf("0");        }        printf("\n");    }    return 0;}void Reverse(std::string& str){    for (unsigned int i=0; i<str.size()/2; ++i)    {        std::swap(str[i], str[str.size()-1-i]);    }}std::string Add(const std::string& strA, const std::string& strB, bool& Flag, bool b){    if (strA.empty() || strB.empty())    {        std::string strSum;        if (!strA.empty())        {            strSum = strA;        }        else if (!strB.empty())        {            strSum = strB;        }        else        {            strSum = "";        }        Reverse(strSum);        for (unsigned int i=0; i<strSum.size(); )        {            if (strSum.at(i) == 0 || strSum.at(i) == 0)            {                i = 0;                strSum.erase(strSum.begin());            }            else            {                ++i;                break;            }        }        Reverse(strSum);        return strSum;    }    else    {        char szTemp[SIZE] = {0};        for (unsigned int i=0, j=0; i<strA.size(); ++i, ++j)        {            szTemp[j] =  strA[i] - 0;        }        for (unsigned int i=0, j=0; i<strB.size(); ++i, ++j)        {            szTemp[j] += strB[i] - 0;        }        int nLength = std::max(strA.size(), strB.size());        std::string strSum(szTemp, szTemp+nLength);        strSum.resize(strSum.size()+1);        if (!b)        {            for (unsigned int i=0; i<strSum.size()-1; ++i)            {                strSum.at(i+1) += strSum.at(i)/10;                strSum.at(i) = strSum.at(i)%10 + 0;            }            strSum.at(strSum.size()-1) = strSum.at(strSum.size()-1)%10 + 0;        }        else        {            for (unsigned int i=strSum.size()-1; i>=0; --i)            {                if (0 == i)                {                    if (strSum.at(i)/10 > 0)                    {                        Flag = true;                    }                    strSum.at(i) = strSum.at(i)%10 + 0;                    break;                }                else                {                    strSum.at(i-1) += strSum.at(i)/10;                    strSum.at(i) = strSum.at(i)%10 + 0;                }            }        }        Reverse(strSum);        for (unsigned int i=0; i<strSum.size(); )        {            if (strSum.at(i) == 0 || strSum.at(i) == 0)            {                i = 0;                strSum.erase(strSum.begin());            }            else            {                ++i;                break;            }        }        Reverse(strSum);        return strSum;    }}

 

HDU-1753