ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1048. Superlong Sums

Why I got WA?
Posted by chinachen 8 Apr 2002 18:48
my program as follow:

#include <iostream.h>
void main()
{
    static long int n,a[112000],b[112000],i,t=0;
    int a1,b1,t1;
    cin>>n;
    t1=n%9;
    for(i=1;i<=n%9;i++)
    {
        cin>>a1>>b1;
        a[0]*=10;
        b[0]*=10;
        a[0]+=a1;
        b[0]+=b1;
    }
    if(n%9==0) t=0;
    else t=1;
    for(;i<=n;i++)
    {
        cin>>a1>>b1;
        a[t]*=10;
        b[t]*=10;
        a[t]+=a1;
        b[t]+=b1;
        if((i-t1)%9==0)
            t++;
    }
    for(i=t-1;i>0;i--)
    {
        a[i-1]+=(a[i]+b[i])/1000000000;
        a[i]=(a[i]+b[i])%1000000000;
    }
    a[0]=a[0]+b[0];
    cout<<a[0];
    for(i=1;i<t;i++)
    {
        cout.fill('0');
        cout.width (9);
        cout<<a[i];
    }
}

I store 9 digits in a long type.What's wrong with it?
Can someone give me some test data
And I too. Why ?
Posted by sikee8 17 Apr 2002 14:32
i don't know whats wrong your program. as my.


#include <iostream.h>

int main ()
{
    bool first = true;
    long n,n9,a,b,back=0,i=0;
    cin >> n;
    while  (i<=n)
    {
        n9=-1;

        do
        {
            cin >> a >> b;
            i++;
            if (first && a==0 && b==0)
                continue;
            n9++;
        } while ( ((a+b) == 9 || (first && !a && !b)) &&
(i<n) );
        if (a+b>9)
        {
            cout << back + 1;
            for (long j=1; j<=n9; j++) cout << 0;
        }
        if (a+b<9)
        {
            if (!first) cout << back;
            for (long j=1; j<=n9; j++) cout << 9;
        }
        back = (a+b)%10;
        if (i==n)
        {
            if (a+b==9)
            {
                if (!first) cout << back;
                for (long j=1; j<=n9; j++) cout << 9;
            }
            cout << back;
            return 0;
        }
        first = false;
    }
    return 0;  //never
}