HDU 1715 大菲波数

/*
这道题的收获,当你不知道数据需要定多大时,输出最大数据自己进行一次判断,不要盲目带数据。如①处的语句。
*/
#include <cstdio>
#include <cstring>
const int nMax = 1007;
const int mMax = 250;
struct BigNumber
{
	int data[mMax];
	int len;
	BigNumber(){len = 1; memset(data, 0 ,sizeof(data));}
	BigNumber(char *str){*this = str;}
	BigNumber &operator=(char *str);
	BigNumber operator+(BigNumber &a);
	void clearLeadZero();
	void print();
}F[nMax];
BigNumber & BigNumber::operator =(char *str)
{
	memset(data, 0, sizeof(data));
	len = strlen(str);
	int i;
	for(i = 0; i < len; ++ i)
		data[i] = str[len - i - 1] - '0';
	return *this;
}
BigNumber BigNumber::operator +(BigNumber &a)
{
	BigNumber z;
	int i, j, q;
	q = 0;
	for(i = 0, j = 0; q || i < len || j < a.len; ++ i, ++ j)
	{
		int p = data[i] + a.data[j] + q;
		z.data[i] = p % 10;
		q = p / 10;
	}
	z.len = i;
	z.clearLeadZero();
	return z;
}
void BigNumber::clearLeadZero()
{
	while(len > 1 && !data[len - 1]) -- len;
}
void BigNumber::print()
{
	int i;
	for(i = len - 1; i >= 0; -- i)
		printf("%d", data[i]);
	printf("\n");
}
bool isZero(int pi)
{
	if(F[pi].data[0] == 0 && F[pi].len == 1)
		return 1;
	return 0;
}
BigNumber &f(int pi)
{
	if(!isZero(pi)) return F[pi];
	if(pi == 1 || pi == 2) F[pi] = "1";
	else
		F[pi] = f(pi - 1) + f(pi - 2);
	return F[pi];
}
int main()
{
	freopen("f://data.in", "r", stdin);
	int T;
	scanf("%d", &T);
	int N;
	while(T--)
	{
		scanf("%d", &N);
		f(N);
		F[N].print();
		//printf("%d\n",F[N].len);//①
	}	
	return 0;
}

你可能感兴趣的:(struct,qq)