Cod sursa(job #561082)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 18 martie 2011 20:50:03
Problema Next Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>

using namespace std;

int a[1000030], n;
long long d;
char s[1000002];

void Elim0 ()
{
	int i, k=0; 
	for (i=0; a[i]==0; i++);
	k=i;
	n-=k;
	if (n==0)
	{
		n=1;
		a[0]=0;
	}
	else
		if (k!=0)
			for (i=0; i<n; i++)
				a[i]=a[i+k];		
}

int Impartire ()
{
	int x, i, y;
	x=0;
	for (i=0; i<n; i++)
	{
		x=x*10+a[i];
		y=(long long)x/d;		
		a[i]=y;
		x=x-y*d;
	}
	Elim0 ();
	return x;
}

void Adunare1 ()
{
	int i;
	i=0;
	a[i]++;
	while (a[i]==10)
	{
		a[i]=0;
		a[++i]++;
	}
	if (i==n)
		n++;
}

void Inversare ()
{
	int i, j, aux;
	for (i=0, j=n-1; i<j; i++, j--)
	{
		aux=a[i];
		a[i]=a[j];
		a[j]=aux;
	}	
}

void Inmultire ()
{
	Inversare ();
	int t=0, ceadun, i;
	for (i=0; i<n; i++)
	{
		ceadun=a[i]*d+t;
		a[i]=ceadun%10;
		t=ceadun/10;
	}
	while (t)
	{
		a[i++]=t%10;
		t/=10;
	}
	n=i;
}

int main ()
{
	ifstream f("next.in");
	f>>s;
	f>>d;
	f.close();
	int i;
	n=strlen (s);
	for (i=0; i<n; i++)
		a[i]=s[i]-'0';
	if (Impartire ()==0)
	{
		ofstream g("next.out");
		g<<s<<"\n";
		g.close();
		return 0;
	}
	Adunare1 ();
	Inmultire ();
	ofstream g("next.out");
	for (i=n-1; i>-1; i--)
		g<<a[i];
	g<<"\n";
	g.close();
	
	return 0;
}