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 WA test 2)
Posted by fsb4000 30 Nov 2010 20:05
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
using namespace std;

typedef vector<long> lnum;
const long base=1000*1000*1000;


void print(lnum a){
    printf ("%d", a.empty() ? 0 : a.back());
    for (int i=(int)a.size()-2; i>=0; --i)
    printf ("%09d", a[i]);
}


void a_plus_b_a(lnum &a, lnum b){
    long carry = 0;
        for (size_t i=0; i<max(a.size(),b.size()) || carry; ++i)
        {
            if (i == a.size())
            {
                a.push_back (0);
            }
            a[i] += carry + (i < b.size() ? b[i] : 0);
            carry = a[i] >= base;
            if (carry)
            {
                a[i] -= base;
            }
        }
}


lnum string_to_Super_long_number(string s){
    lnum a;
    for (int i=(int)s.length(); i>0; i-=9)
    {
        if (i < 9)
        {
            a.push_back (atoi (s.substr (0, i).c_str()));
        }
        else
        {
            a.push_back (atoi (s.substr (i-9, 9).c_str()));
        }
    }
    while (a.size() > 1 && a.back() == 0){
        a.pop_back();
    }
    return a;
}



int main(void){
    lnum a,b;
    long i;
    long m;
    char c;
    string s1="",s2="";
    cin>>m;
    for(i=0;i<m;i++)
    {
        cin>>c;
        s1=s1+c;
        cin>>c;
        s2=s2+c;
    }
    a=string_to_Super_long_number(s1);
    b=string_to_Super_long_number(s2);
    a_plus_b_a(a,b);
    print(a);
    cout<<endl;
    system("pause");
    return 0;
}