Cod sursa(job #2113417)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 24 ianuarie 2018 15:50:50
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <cstring>
#include <cctype>

using namespace std;

ifstream f("next.in");
ofstream g("next.out");

long long int n,m,d=0;
short a[2000005],b[2000005],c[2000005];
long long int adun;
char c1;
long long int t,i,j;

int main()
{

    n=0;
    m=0;
    while(f.get(c1)&&isdigit(c1))
    {
        a[++n]=(c1-'0');
        b[++m]=a[n];
    }
    a[0]=n;
    b[0]=n;
    f>>d;
    for(i=1; i<=n/2; i++)
    {
        swap(a[i],a[n-i+1]);
        swap(b[i],b[n-i+1]);
    }

    t=0;
    i=a[0];
    while(i>0)
    {
        t=t*10+a[i];
        a[i]=t/d;
        t=t%d;
        i=i-1;
    }
    while(a[0]>0&&a[a[0]]==0)
        a[0]=a[0]-1;

    if(t==0)
        for(i=b[0]; i>=1; i--)
            g<<b[i];
    else
    {
        adun=(d-t)%d;
        t=0;
        i=1;
        while(i<=b[0]||adun||t>0)
        {
            t=t+b[i]+adun%10;
            c[i]=t%10;
            t=t/10;
            adun/=10;
            i=i+1;
        }
        c[0]=i-1;
        for(i=c[0]; i>=1; i--)
            g<<c[i];
    }
    g<<'\n';
    return 0;
}