১৪ মার্চ ২০১৬

Big Integer Sum between Two Numbers

#include<bits/stdc++.h>

using namespace std;

void x_y(string a, string b)
{
    int alen=a.size(), blen=b.size(),i,j, sum, carry=0, num[1009], numlen=0;

    i=alen-1;
    j=blen-1;

    for(; i>=0; i--)
    {
        sum = a[i]-48;

        if(j>=0)
        {
            sum+=(b[j]-48);
            j--;
        }

        sum+=carry;

        if(sum > 10)
        {
            num[numlen]=sum%10;
            carry=sum/10;
        }
        else
        {
            num[numlen]=sum%10;
            carry=sum/10;
        }

        numlen++;
    }

//    if(carry)
//    {
//        cout << carry;
//    }

    if(carry)
    {
        num[numlen++]=carry;
    }

    for(i=numlen-1; i>=0; i--)
    {
        cout << num[i];
    }

    cout << endl;
}

void eql(string a, string b)
{
    int alen=a.size(), blen=b.size(), i, j, sum, carry=0, num[1009], numlen=0;

    i=alen-1;
    j=blen-1;

    for(; i>=0 and j>=0; i--,j--)
    {
        sum = (a[i]-48) + (b[j]-48);

        sum+=carry;

        if(sum > 10)
        {
            num[numlen]=sum%10;
            carry=sum/10;
        }

        else
        {
            num[numlen]=sum%10;
            carry=sum/10;
        }

        numlen++;
    }

    if(carry)
    {
        cout << carry;
    }

    for(i=numlen-1; i>=0; i--)
    {
        cout << num[i];
    }

    cout << endl;
}

int main()
{
    string x,y;

    while(cin >> x >> y)
    {
        if(y.size() > x.size())
        {
            swap(x,y);
        }

        if(x.size() > y.size())
        {
            x_y(x,y);
        }

        else
        {
            eql(x,y);
        }
    }

    return 0;
}

1 টি মন্তব্য:

  1. MGM Resorts completes $325M expansion project in Las Vegas
    MGM Resorts International said Thursday 충주 출장샵 that 의정부 출장마사지 it completed 속초 출장마사지 the $325 million 전라남도 출장안마 expansion of 파주 출장샵 its MGM Resorts International property in Mar 11, 2021

    উত্তরমুছুন