现在的位置: 首页 > 综合 > 正文

Hdu 1250 Hat’s Fibonacci

2019年04月08日 ⁄ 综合 ⁄ 共 1097字 ⁄ 字号 评论关闭

唔,bign类高精度还是蛮好用的。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

const int MAXN = 2010;
int n;

struct bign
{
	int len, s[MAXN];
	bign ()
	{
		memset(s, 0, sizeof(s));
		len = 1;
	}
	bign (int num) {*this = num;}
	bign (const char *num) { *this = num;}
	bign operator = (const char *num)
	{
		len = strlen(num);
		for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
		return *this;
	}
	bign operator = (int num)
	{
		char s[MAXN];
		sprintf(s, "%d", num);
		*this = s;
		return *this;
	}
	bign operator +(const bign &b) const
	{
		bign c;
		c.len = 0;
		for(int i = 0, g = 0; g || i < max(len, b.len); i++)
		{
			int x = g;
			if(i < len) x += s[i];
			if(i < b.len) x += b.s[i];
			c.s[c.len++] = x%10;
			g = x / 10;
		}
		return c;
	}
	bool operator < (const bign &b) const
	{
		if(len != b.len) return len < b.len;
		for(int i = len-1; i >= 0; i--)
		{
			if(s[i] != b.s[i]) return s[i] < b.s[i];
		}
		return false;
	}
	void print()
	{
		for(int i = len-1; i >= 0; i--)
		{
			printf("%d", s[i]);
		}
		printf("\n");
	}	
};


void solve()
{
	if(n == 1) { printf("1\n"); return ;}
	if(n == 2) { printf("1\n"); return ;}
	if(n == 3) { printf("1\n"); return ;}
	if(n == 4) { printf("1\n"); return ;}
	bign f1 = 1, f2 = 1, f3 = 1, f4 = 1, f5 = 1;
	for(int i = 5; i <= n; i++)
	{
		f5 = f1+f2+f3+f4;
		f1 = f2;
		f2 = f3;
		f3 = f4;
		f4 = f5;
	}
	f5.print();
}

int main()
{
	while(~scanf("%d", &n))
	{
		solve();
	}
	return 0;
}

抱歉!评论已关闭.