Cod sursa(job #1322405)

Utilizator get0000lostSatmarean Paul get0000lost Data 20 ianuarie 2015 00:12:56
Problema Al k-lea termen Fibonacci Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.01 kb
#include <stdio.h>

long long *a;

////keeps window open
//void keepwindowopen()
//{
//	printf("\n");
//	printf("Press e key to continue...");
//	char s[2] = "";
//	_getch(s);
//}

//computes the xth term of the fibbonacci series
long long fibo(int x)
{
	if (x == 0) return 0;
	if (x == 1 || x == 2)
		return 1;
	else
	{
		long long b = 0;
		if (a[x] != 0)
			return a[x];
		else
			b = fibo(x - 1) + fibo(x - 2);
			a[x] = b;
			return b;
	}
}

long long* init(int s)
{
	long long *a;
	a = (long long*)malloc((s + 1) * sizeof(long long));
	int i;
	for (i = 0; i < s+1; i++)
		a[i] = 0;
	return a;
}



int main()
{
	int x;
	long long fx;
	FILE *pf;
	pf = fopen("kfib.in", "r");
	
	fscanf(pf,"%d", &x);
	
	fclose(pf);


	/*printf("introduceti termenul: ");
	scanf_s("%d", &x);*/
	
	a = init(x);
	fx = fibo(x);


	printf("the %dth term is: %lld", x, fx);
	//keepwindowopen();


	pf = fopen("kfib.out", "w");
	fprintf(pf, "%lld", fx);
	fclose(pf);
	free(a);
	return 0;
}