Calculate N using a high-precision method! And show n! (Factorial) value.

Source: Internet
Author: User

[Problem description]
For any given n value (N is an integer and 1 <= n <= 100), calculate and display n! (Factorial) value.
[Example]
Input: n = 10
Output: 3628800
Input: n = 20
Output: 2432902008176640000

Use an integer array to store every bit of a large number class and simulate the whole process of manual multiplication ..

# Include "stdio. H "# include" stdlib. H "const unsigned int max = 10000; // the maximum length of the integer array const long widthmax = 1000000000; // The maximum element width of the integer array Val [Max] is const unsigned int width = 9; // The format width of the output element of the integer array Val [Max, that is, the maximum number of digits of the elements of the integer array Val [Max] typedef struct node {long Val [Max]; // used to store the high-precision Integer unsigned int size; // actual length of the integer array} bigint; void printbigint (const bigint & A); // output bigint mulbigint (const bigint & A, const bigint & B); // multiply bigint facbigint (unsigned int N) by a big number class; // calculate the factorial void printbigint (const bigint & A) {unsigned W; int I; printf ("% LLD",. val [. size-1]); for (I =. size-2; I> = 0; I --) {W = widthmax/10; while (W> 0) {if (. val [I]> = W) break; printf ("0"); W/= 10;} printf ("% LLD",. val [I]);} printf ("\ n");}/* function name: mulbigint function: high-precision integer multiplication input parameter: const bigint &: high-precision Integer multiplier const bigint & B: output parameter of the high-precision Integer multiplier represented by an integer array: Bigi NT: return the high-precision Integer product */bigint mulbigint (const bigint & A, const bigint & B) {int I, j; bigint C; if (. size = 1 &. val [0] = 0) return a; If (B. size = 1 & B. val [0] = 0) return B; for (I = 0; I <Max; I ++) // All values are 0 C. val [I] = 0; for (I = 0, j = 0; I <B. size; I ++) {for (j = 0; j <. size; j ++) {C. val [I + J] + =. val [J] * B. val [I]; C. val [I + J + 1] + = C. val [I + J]/widthmax; C. val [I + J] % = widthmax;} C. size = I + J; If (C. VA L [C. Size]! = 0) // The highest bit has an inner C. size ++;} return C;}/* function name: facbigint function: high-precision Integer factorial input parameter: Unsigned int N: Positive Integer output parameter: bigint: returns the high-precision Integer factorial */bigint facbigint (unsigned int N) {unsigned long I; bigint S, C; C. size = S. size = 1; S. val [0] = 1; for (I = 2; I <= N; I ++) {C. val [0] = I; S = mulbigint (S, C);} return s;} int main (void) {bigint A; unsigned int N; printf ("Enter N value:"); scanf ("% u", & N); A = facbigint (n); printf ("% u's factorial is: \ n ", n); printbigint (a); System (" pause "); Return 0 ;}

Method 2:
 

# Include <iostream> using namespace STD; # define MAX 1000 int main (void) {int N; while (scanf ("% d", & N) = 1 & n> = 0) {int I, j; int A [Max]; // save count calculation result int P, add; // P stores the number of digits of the current result. Add is carry a [1] = 1; P = 1; for (I = 2; I <= N; ++ I) // loop with 2, 3, 4 ..... N multiplication {for (j = 1, add = 0; j <= P; ++ J) // multiply each of a [] by I {A [J] = A [J] * I + Add; add = A [J]/10; A [J] = A [J] % 10;} while (add> 0) // If H is not 0 {A [J] = add % 10; add = Add/10; + + J;} p = J-1; // assign the current number of digits to p} for (I = P; I> = 2; -- I) // the front of a [] array is a low position, followed by a high position {printf ("% d", a [I]);} printf ("% d \ n ", A [I]);} return 0 ;}

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.