Cod sursa(job #2393043)

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

using namespace std;

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


void h_adun(int a[],int b[],int c[])
{
    int i,tr=0,aux;
    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]);
}
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);
    c[0]=a[0];
    while(h_cmp(a,c)>=0)
    {
        h_adun(c,b,c);
    }
    while(c[c[0]]==0)
        c[0]--;
    print(c);
    return 0;
}