Cod sursa(job #118219)

Utilizator megabyteBarsan Paul megabyte Data 23 decembrie 2007 18:44:41
Problema Nunta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
#include <string>
#define NMAX 1024
#define INF "nunta.in"
#define OUF "nunta.out"
using namespace std;
struct bint
{
	short dim;
	char c[254];
	void init(short nr)
	{
		short i;dim=0;
		memset(c,0,254);
		while(nr)
		{
			c[++dim]=nr%10;
			nr/=10;
		}
	}
	void print()
	{
		short i;
		i=dim;
		while(i) printf("%hd",c[i--]);
	}
};

bint sum(bint a,bint b)
{
	bint c;
	short i,d1,d2;
	char t=0;
	i=1;d1=a.dim;d2=b.dim;
	while(i<=d1||i<=d2||t>0)
	{
		t+=a.c[i]+b.c[i];
		c.c[i]=t%10;
		++i;t/=10;
	}
	c.dim=i-1;
	return c;
}

int main()
{
	int t[NMAX],i,n;
	FILE *in,*out;
	bint alfa,beta,tau,*p,*p1,*p2,*aux;
	in=fopen(INF,"r");
	out=fopen(OUF,"w");
	fscanf(in,"%d",&n);

	alfa.init(1);
	beta.init(2);
	p2=&alfa;p1=&beta;p=&tau;
	if(n==1) fprintf(out,"1\n");
	else if(n==2) fprintf(out,"2\n");
	else
	{
		for(i=3;i<=n;++i)
		{
			*p=sum(*p1,*p2);
			aux=p2;p2=p1;p1=p;p=aux;
		}
		i=p1->dim;
		while(i) fprintf(out,"%hd",p1->c[i--]);
	}
	
	fclose(in);fclose(out);
	return 0;
}