50
30414093201713378043612608166064768844377641568960512000000000000
解析:在开数组的时候一定要开大一点,我开的小怎么都不能通过,气死我了,,,大数的思路就是把大数存放在数组中,代码如下,两个代码思路都是一样的,一个是从后往前存,一个是从前往后存!
代码1:
# include <stdio.h> # define N 16400 int main(void) { int i,j,m,a[16401] = {0}; a[N] = 1; scanf("%d", &m); for (i = 1; i <= m; i++) { for (j= N; j >= 1; j--) { a[j] = a[j] * i; } for (j = N; j >= 1; j--) { while (a[j] > 9) { a[j-1] += a[j]/10; a[j] = a[j]%10; j--; } } } i = 1; while (a[i] == 0) i++; for (j = i; j <= N; j++) printf("%d", a[j]); printf("\n"); return 0; }
代码2:# include <stdio.h> # define N 19000 int main(void) { int i,j,m,c,s,a[19001] = {0}; a[1] = 1; scanf("%d", &m); for (i = 1; i <= m; i++) { c = 0; for (j = 1; j <= N; j++) { s = a[j] * i + c; a[j] = s % 10; c = s/10; } } i = N; while (a[i] == 0) i--; for (j = i; j>=1; j--) printf("%d",a[j]); printf("\n"); return 0; }
最优代码:#include<iostream>
02.
#include<iomanip>
03.
using
namespace
std;
04.
//存储20000以内的阶乘
05.
int
a[15470];
06.
int
main()
07.
{
08.
//freopen("1.txt","r",stdin);
09.
//freopen("2.txt","w",stdout);
10.
int
n;
11.
cin>>n;
12.
a[1]=1;
13.
a[0]=1;
14.
int
up;
15.
for
(
int
i=2;i<=n;++i)
16.
{
17.
up=0;
18.
for
(
int
j=1;j<=a[0];++j)
//各个位相乘
19.
{
20.
a[j] *=i;
21.
a[j] +=up;
22.
up=a[j]/100000;
23.
a[j] %=100000;
24.
}
25.
if
(up!=0)
26.
{
27.
a[0]++;
28.
a[a[0]]=up;
29.
}
30.
}
31.
if
(a[0]==1) cout<<a[1];
32.
else
33.
{
34.
cout<<a[a[0]];
35.
for
(
int
i=a[0]-1;i>0;i--)
36.
{
37.
cout<<setfill(
‘0‘
)<<setw(5)<<a[i];
38.
}
39.
}
40.
}
原文地址:http://blog.csdn.net/java_oracle_c/article/details/41911275