POJ_1503

一.题目

Integer Inquiry
Time Limit: 1000MS
Memory Limit: 10000K

Description

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670


二.解题技巧

    这道题是一个简单的无符号大数字相加,通过模拟手算就可以实现,主要技巧在于需要将输入的大数字按位逆序放在vector或数组中,也就是将个位的数字放在下标为0的位置,将十位上的数字放在下标为1的位置,这样子放置的话,发现结果有进位的时候,只要在vector或者数组后面添加一位就可以了。


三.实现代码

#include <iostream>
#include <string>
#include <vector>

using namespace std;



class Highprecision
{
    private:
        std::vector<signed char> Element;
        Highprecision();
        void ConvertString(const std::string& InputString);
        void ConvertInteger(long InputInteger);

    public:
        Highprecision(const std::string& InputString);
        Highprecision(long InputInteger);
        Highprecision(const Highprecision& InputHighrecision);


        // supported operator
        friend std::ostream& operator << (std::ostream& OutputStrem,
                                          Highprecision& OutputHighprecision);
        Highprecision operator + (const Highprecision& Rhs);
        Highprecision& operator = (const Highprecision& Rhs);
};

Highprecision::Highprecision():Element()
{
}

void Highprecision::ConvertString(const std::string &InputString)
{
    if (!Element.empty())
    {
        Element.clear();
    }

    int Length = InputString.size();
    const char* Array = InputString.c_str();
    signed char Tmp = 0;

    for (int Index = 1; Index <= Length; Index++)
    {
        Tmp = (*(Array + Length - Index)) - '0';
        Element.push_back(Tmp);
    }
}





void Highprecision::ConvertInteger(long InputInteger)
{
    if (!Element.empty())
    {
        Element.clear();
    }

    unsigned char Value = 0;
    while(true)
    {
        Value = InputInteger % 10;
        Element.push_back(Value);
        InputInteger = InputInteger / 10;
        if (InputInteger == 0)
        {
            break;
        }

    }
}




Highprecision::Highprecision(const std::string &InputString):Element()
{
    ConvertString(InputString);
}





Highprecision::Highprecision(long InputInteger) : Element()
{
    ConvertInteger(InputInteger);
}



Highprecision::Highprecision(const Highprecision &InputHighrecision) :
    Element(InputHighrecision.Element)
{
}




std::ostream& operator <<(std::ostream &OutputStream, Highprecision &OutputHighprecision)
{
    std::vector<signed char> Element = OutputHighprecision.Element;
    int Size = Element.size();
    signed char Tmp = 0;

    for (int Index = 1; Index <= Size; Index++)
    {
        Tmp = Element[Size - Index] + '0';
        OutputStream << Tmp;
    }
    return OutputStream;
}




Highprecision Highprecision::operator +(const Highprecision &Rhs)
{
    signed char LValue = 0;
    signed char RValue = 0;
    signed char Carry = 0;


    int LSize = Element.size();
    int RSize = Rhs.Element.size();


    const std::vector<signed char>& RElement = Rhs.Element;
    signed char TmpResult = 0;

    Highprecision Result;
    std::vector<signed char>& ResultElement = Result.Element;


    for (int Index = 0; (Index < LSize) || (Index < RSize); Index++)
    {
        if (Index < LSize)
        {
            LValue = Element[Index];
        }
        if (Index < RSize)
        {
            RValue = RElement[Index];
        }

        TmpResult = LValue + RValue + Carry;
        Carry = TmpResult / 10;
        TmpResult = TmpResult % 10;
        ResultElement.push_back(TmpResult);
        LValue = 0;
        RValue = 0;
    }

    if (Carry > 0)
    {
        ResultElement.push_back(Carry);
    }


    return Result;

}



Highprecision &Highprecision::operator =(const Highprecision &Rhs)
{
    if (&Rhs == this)
    {
        return *this;
    }


    Element = Rhs.Element;
    return *this;

}


int main()
{

    Highprecision Result(0);
    std::string InputString;

    while(true)
    {
        cin >> InputString;
        if ((InputString.size() == 1) && (InputString[0] == '0'))
        {
            break;
        }

        Highprecision Input(InputString);
        Result = Result + Input;
    }

    cout << Result;
    return 0;
}



四.体会

    这个题目还是比较基础的,只要有了思路,剩下来的就是执行力了。


 

版权所有,欢迎转载,转载请注明出处,谢谢微笑



你可能感兴趣的:(C++,poj)