/*
曾經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; ++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;
}
摘自 lhshaoren