hdu 1042 N!

/*
曾经WA了N多次,错误出现在①处,需要在②处进行补充
*/
#include <cstdio>
#include <cstring>
const int nMax = 50000;
struct BigNumber
{
	int data[nMax];
	int len;
	BigNumber(){len = 1;memset(data,0,sizeof(data));}
	BigNumber & operator=(const char *str);
	BigNumber operator*(int a);//这里乘数只需要是整数即可
	void print();
	void clearLeadZero();
}ans;
void BigNumber::clearLeadZero()
{
	while(len > 1 && !data[len - 1]) len--;
}
BigNumber & BigNumber::operator=(const 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*(int a)
{
	int q = 0;
	BigNumber z;
	int i;
	for(i = 0; i < len; ++ i)
		//①这是正解,但是for(i = 0; i < len + 10; ++i)然后去掉while()循环判断然后就会错误!答案在②处
	{
		int p = data[i] * a + q;
		z.data[i] = p % 10;
		q = p / 10;
	}
	while(q)
	{
		z.data[i++] = q % 10;
		q /= 10;
	}
	z.len = i;
	z.clearLeadZero();
	return z;
}
void BigNumber::print()
{
	int i;
	for(i = len - 1; i >= 0; -- i)
		printf("%d",data[i]);
	printf("\n");
}
int N;
int main()
{
	//freopen("f://data.in", "r", stdin);
	//freopen("f://data2.out", "w", stdout);
	while(scanf("%d",&N) != EOF)
	{
		ans  = "1";
		int i;
		for(i = 2; i <= N; ++ i)
			ans = ans * i;
		ans.print();
	}
	return 0;
}

你可能感兴趣的:(struct)