Description We all know how to calculate the factorial of a number, but if the number is large, how do we calculate it and output it?
-
Input: Enter an integer m (0<m<=5000)
-
Output: The factorial of the output m, and enter a newline character after the output ends
-
Example input: 50
-
Sample output: 30414093201713378043612608166064768844377641568960512000000000000
-
Bo Main analysis: The subject seems to be very simple, the operation of a little trouble, but also to pay attention to some details ·, because the factorial of 5000 is too large, so can not be used as a number of output, a similar conversion into the system,
-
Here I used 10000 binary, with the array to represent each bit of the number, it is easy to deal with, and pay attention to where the factorial to terminate,
-
Reference code:
-
#include <stdio.h>
#include <math.h>
int a[6800];
int Wu (int j)//Determine the number of arrays to be used
{
int i,s;s=j;
For (i=0;i<1000;i++)
if (a[j+i+1]!=0)
Break ;
if (i==1000)
return (0);
else return (1);
}
int bei (int n,int j,int c)//calculate each one with the recursive principle
{
int m,i,k;
A[j]=a[j]*n+c;
c=a[j]/10000;
a[j]=a[j]%10000;
if (Wu (j) | | | c!=0)
return (bei (N,j+1,c));
else Return (j);
}
Main ()
{
int i,j,k,m;
scanf ("%d", &m);
a[0]=1;
For (i=1;i<=m;i++)
K=bei (i,0,0);
printf ("%d", a[k]);
for (i=k-1;i>=0;i--)//output to the corresponding complement 0
{if (a[i]<10000&&a[i]>999)
printf ("%d", A[i]);
else if (a[i]<1000&&a[i]>99)
printf ("0%d", A[i]);
else if (a[i]<100&&a[i]>9)
printf ("00%d", A[i]);
Else
printf ("000%d", A[i]);}
Putchar (' \ n ');
}
-
Large number factorial