Cod sursa(job #2393005)

Utilizator REDCRAFTPadure Damian REDCRAFT Data 30 martie 2019 18:22:17
Problema Next Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMAX=1000005;
const int MMAX=20;

int h_rest(int a[], int x)
{
    int i;
    int r=0;
    for(i=a[0];i>=1;--i)
        r=(10*r+a[i])%x;
    return r;
}
void h_scad(int a[],int b[],int c[NMAX])
{
    int i,imp,aux;
    memset(c,0,sizeof(c));
    c[0]=a[0];
    imp=0;
    for(i=1;i<=c[0];i++)
    {
        aux=a[i]-b[i]-imp;
        if(aux<0)
        {
            aux=aux+10;
            imp=1;
        }
        else
            imp=0;
        c[i]=aux%10;
    }
    while(c[0]>1)
        --c[0];
    if(c[0]==0)
        c[0]++;
}
void h_adun(int a[],int b[],int c[])
{
    int i,tr,aux;
    memset(c,0,sizeof(c));
    c[0]=max(a[0],b[0]);
    tr=0;
    for(i=1;i<=c[0];i++)
    {
        aux=a[i]+b[i]+tr;
        c[i]=aux%10;
        tr=aux/10;
    }
}
int h_cmp(int a[],int b[])
{
    int i;
    if(a[0]>b[0])
        return 1;
    if(a[0]<b[0])
        return -1;
    for(i=a[0];i>=1;i--)
    {
        if(a[i]>b[i])
            return 1;
        if(a[i]<b[i])
            return -1;
    }
    return 0;
}
void print(int a[])
{
    int i;
    for(i=a[0];i>=1;i--)
        printf("%d",a[i]);
    printf("\n");
}
int a[NMAX],b[MMAX],c[NMAX];
int main()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    char ch;
    int n,i,k,cnt=0;
    while(scanf("%c",&ch)&&ch!='\n')
    {
        if(ch>='0'&&ch<='9')
            a[++a[0]]=ch-'0';
    }
    reverse(a+1,a+a[0]+1);
    while(scanf("%c",&ch)&&ch!='\n')
    {
        if(ch>='0'&&ch<='9')
            b[++b[0]]=ch-'0';
    }
    reverse(b+1,b+b[0]+1);
    while(h_cmp(a,c)>=0)
    {
        h_adun(c,b,c);
    }
    print(c);
    return 0;
}