4-10 factorial calculation upgraded version (20 points), 4-10 factorial

Source: Internet
Author: User

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 );

WhereNIs a user-passed parameter. Its value cannot exceed 1000. IfNIf 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 !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.