4-10 factorial calculation upgraded version (20 points), 4-10 factorial
This question requires a function to print non-negative integer factorial.
Function interface definition:
void Print_Factorial ( const int N );
WhereN
Is a user-passed parameter. Its value cannot exceed 1000. IfN
If it is a non-negative integer, the function must be printed in one row.N
! Otherwise, "Invalid input" is printed ".
Example of the referee test procedure:
# Include <stdio. h> void Print_Factorial (const int N); int main () {int N; scanf ("% d", & N); Print_Factorial (N); return 0 ;} /* your code will be embedded here */
Input example:
15
Output example:
1307674368000
Notes and ideas for solving problems:
When I first thought about this question, I ignored the important condition of the input integer <1000, so I used the unsigned long int to calculate it directly, but after I handed it in, partially correct (SADLY ). The factorial of 1000. This number is very large,
It has exceeded the range of the unsigned long type, so it can only be used as an array. The number of factorial digits of 1000 is about 2700, so the open point of the array is slightly larger (3000 ).
Code display:
Void Print_Factorial (const int N)
{
Int sum [3000] = {0 };
Sum [0] = 1;
Int I, j;
Int n = 0, numofwei = 1, tmp;
If (N> 0)
{
For (I = 2; I <= N; I ++)
{
For (j = 0; j <numofwei; j ++)
{
Tmp = sum [j] * I + n;
Sum [j] = tmp % 10;
N = tmp/10;
If (n & j = numofwei-1)
Numofwei ++;
}
}
For (I = numofwei-1; I> = 0; I --)
Printf ("% d", sum [I]);
Printf ("\ n ");
}
Else if (N = 0)
Printf ("1 \ n ");
Else
Printf ("Invalid input \ n ");
}
Note: initialization of variables !!!