hdu - 1042 - N!(高精度计算)

时间:2022-10-03 03:38:31

题意:求N!(0 <= N <= 10000)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

——>>简单高精度运算。只是开始用if来检查最后一个进位,WA了几次,终于发现,最后一位的产生的进位可以超过10,因为乘数不一定是1位数,需用while。

#include <cstdio>

using namespace std;

const int maxn = 40000 + 10;
int s[maxn];

int main()
{
int N, i, j, C, temp;
while(~scanf("%d", &N))
{
s[0] = 1;
int b = 1;
for(i = 2; i <= N; i++)
{
C = 0;
for(j = 0; j < b; j++)
{
temp = s[j] * i + C;
s[j] = temp % 10;
C = temp / 10;
}
while(C > 0)
{
s[b++] = C % 10;
C /= 10;
}
}
for(i = b-1; i >= 0; i--) printf("%d", s[i]);
printf("\n");
}
return 0;
}