poj1503

Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27259   Accepted: 10545

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
水题测板子用的
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 lld;
const int MAXN = 410;
const lld BIT=1000000000;///定义进制
int max(int a,int b)
{
	if(a>b)
		return a;
	return b;
}
struct bign
{
    int len, s[MAXN];
    bign ()
    {
        memset(s,0, sizeof(s));
        len = 1;//从1开始
    }
    bign (int num) { *this = num; }
    bign (const char *num) { *this = num; }
   void print(bool flag=false)//flag为是否输出某个特定的符号
   {
       int i=len-1;
       printf("%d",s[i]);
       for(i--;i>=0;i--)
       {
           printf("%d",s[i]);
       }
       if(flag)puts("");
   }
    bool zero()//判定,是否是0
    {
        return s[0]==0&&len==1;
    }
    bool one()//判定是否是1
    {
        return s[0]==1&&len==1;
    }

   bign operator = (const int num)
    {

        char s[MAXN];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num)
    {
        int i;
        for( i = 0; num[i] == '0'; num++) ;  //去前导0
        len = strlen(num);
        for( i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
        return *this;
    }
    bign operator + (const bign &b) const //+
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if(i < len) x += s[i];
            if(i < b.len) x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }
     void clr()//全部清空
    {
       memset(s,0,sizeof(s));
       len=1;
    }
    void clean()//清除前面的0
    {
        while(len > 1 && !s[len-1]) len--;
    }
    bign operator * (const bign &b) //
    {
        bign c;
        int i,j;
        c.len = len + b.len;
        for( i = 0; i < len; i++)
        {
            for( j = 0; j < b.len; j++)
            {
                c.s[i+j] += s[i] * b.s[j];
            }
        }
        for( i = 0; i < c.len; i++)
        {
            c.s[i+1] += c.s[i]/10;
            c.s[i] %= 10;
        }
        c.clean();
        return c;
    }
    bign operator *= (const bign &b)
    {
        *this = *this * b;
        return *this;
    }
    bign operator - (const bign &b)
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; i < len; i++)
        {
            int x = s[i] - g;
            if(i < b.len) x -= b.s[i];
            if(x >= 0) g = 0;
            else
            {
                g = 1;x += 10;
            }
            c.s[c.len++] = x;
        }
        c.clean();
        return c;
    }
    bign operator -= (const bign &b)
    {
        *this = *this - b;
        return *this;
    }
    bign operator / (const bign &b)
    {
        bign c, f = 0;
        for(int i = len-1; i >= 0; i--)
        {
            f = f*10;
            f.s[0] = s[i];
            while(f >= b)
            {
                f -= b;c.s[i]++;
            }
        }
        c.len = len;
        c.clean();
        return c;
    }
    bign operator /= (const bign &b)
    {
        *this  = *this / b;
        return *this;
    }
    bign operator % (const bign &b)
    {
        bign r = *this / b;
        r = *this - r*b;
        return r;
    }
    bign operator %= (const bign &b)
    {
        *this = *this % b;
        return *this;
    }
    bool operator < (const bign &b)
    {
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] < b.s[i];
        }
        return false;
    }
    bool operator > (const bign &b)
    {
        if(len != b.len) return len > b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] > b.s[i];
        }
        return false;
    }
    bool operator == (const bign &b)
    {
        return !(*this > b) && !(*this < b);
    }
    bool operator != (const bign &b)
    {
        return !(*this == b);
    }
    bool operator <= (const bign &b)
    {
        return *this < b || *this == b;
    }
    bool operator >= (const bign &b)
    {
        return *this > b || *this == b;
    }
    string str() const
    {
        string res = "";
        for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
        return res;
    }
};
istream& operator >> (istream &in, bign &x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream& operator << (ostream &out, const bign &x)
{
    out << x.str();
    return out;
}
bign lldTobign(lld n)
{
    bign ret;
    int i=0;
    ret.clr();
    if(n==0)return ret;
    while(n)
    {
        ret.s[i]=n%BIT;
        i++;
        n/=BIT;
    }
    ret.len=i;
    return ret;
}
bign CharTobign(char s[])
{
    bign ret;
    ret.clr();
    int i,j,k=strlen(s);
    ret.len=k;
    for(i=k-1,j=0;i>=0;i--,j++)
    {
       ret.s[j]=s[i]-'0';
    }
    return ret;
}
bign BigDiv(bign a,lld b)//大数除小数
{
    int i;
    for(i=a.len-1;i>0;i--)
    {
        a.s[i-1]+=a.s[i]%b*BIT;

        a.s[i]/=b;
    }
    a.s[0]/=b;
    while(a.len>1&&a.s[a.len-1]==0)a.len--;
    return a;
}
bign multi(bign a,lld b)//大数乘小数
{
    int i;
    for(i=0;i<a.len;i++)
    {
        a.s[i]*=b;
    }
    for(i=0;i<a.len;i++)
    {
        if(a.s[i]>=BIT)
        {
            a.s[i+1]+=a.s[i]/BIT;
            a.s[i]%=BIT;
        }
    }
    while(a.s[a.len]>0)
    {
        a.s[a.len+1]+=a.s[a.len]/BIT;
        a.s[a.len]%=BIT;
        a.len++;
    }
    return a;
}
int main()
{
    bign a, b, c;
    c.clr();
    char str[200];
    while(cin>>str)
    {
        a=CharTobign(str);
        if(a.zero())
        {
            break;
        }
        c=a+c;
    }
    c.print(true);
    return 0;
}



你可能感兴趣的:(poj1503)