Cod sursa(job #265086)

Utilizator Bit_MasterAlexandru-Iancu Caragicu Bit_Master Data 23 februarie 2009 11:20:53
Problema Next Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <stdio.h>

const int N = 1000001;

long long d,r,s;
char n[N];
int lim;
long long dif;

void citire()
{
	int i;
	scanf ("%s",n);
	for (i = 0; n [i] != 0; ++i)
		n[i] -= '0';
	lim = i-1;
	scanf ("%lld",&d);
}

void impartire_n_la_d()
{
	for (int poz = 0; poz <= lim; ++poz)
	{
		r = r * 10 + n[poz];
		r = r % d;
	}
}

void adunare_n_plus_s()
{
	for (int poz = lim; poz >= 0; --poz)
	{
		n[poz] = n [poz] + s % 10 + dif;
		dif = n [poz] / 10;
		n [poz] = n [poz] % 10;
		s /= 10;
	}
}

void afisare()
{
	if (dif > 0)
		printf ("%d",dif);
	for (int i = 0; i <= lim; ++i)
		printf ("%d",n[i]);
}

int main()
{
	freopen ("next.in","r",stdin);
	freopen ("next.out","w",stdout);
	citire();
	impartire_n_la_d();
	s = d - r;
	adunare_n_plus_s();
	afisare();
	return 0;
}