码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-1753

时间:2014-08-28 23:58:26      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   2014   div   

/********************************************************************
@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

标签:style   blog   color   os   io   ar   for   2014   div   

原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3943364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!