The exact value of the 5.22 factorial in algorithm competition getting started classic, an algorithm competition getting started classic

Source: Internet
Author: User

The exact value of the 5.22 factorial in algorithm competition getting started classic, an algorithm competition getting started classic

1/* 2 * input a positive integer n up to 1000, and output n! = 1*2*3 *...... * The exact result of n. 3 * sample input: 30 4 * sample output: 265252859812191058636308480000000 5 */6 # include <stdio. h> 7 # include <string. h> 8 # define maxn 3000 9 int f [maxn]; 10 11 int main () 12 {13 int I, j, n; 14 scanf ("% d ", & n); 15 memset (f, 0, sizeof (f); 16 f [0] = 1; 17 for (I = 2; I <= n; I ++) // slave I! Start to count until n! 18 {19 int c = 0; // c indicates the number of incoming bits 20 // multiply the number of bits from the low bits to the high BITs by I, and store the bits in the f [0] (single bits) Order ), f [1] (ten digits), f [2] (hundred digits )...... Simulate a manual computation to get 21 for (j = 0; j <maxn; j ++) 22 {23 int s = f [j] * I + c; 24 f [j] = s % 10; 25 c = s/10; 26} 27} 28 for (j = maxn-1; j> = 0; j --) {if (f [j]) break;} // ignore the leading 029 for (I = j; I> = 0; I --) {printf ("% d ", f [I]);} // output 30 printf ("\ n") in reverse order; 31 return 0; 32} 33/* analysis 34*1. difference between define and const: 35 * define is only used for text replacement. The lifecycle of define constants stops during compilation. It exists in the code segment of the program and is only a constant in the actual program, parameters in a command do not actually exist. 36 * const constants exist in the Data Segment of the program and are allocated space in the stack. const constants exist in the program and can be called and transferred. const constants have data types, macro constants do not have data types. 37 * the compiler can perform type security checks on const constants. 38*2. Due to 1000! If the value is about 4e2567, an array of 3000 elements f is saved in reverse order (easy to carry), that is, f [0] (single digit), f [1] (ten digits ), f [2] (BITs )...... 39*3. Note: if the result itself is 0, ignore the leading 0 to output nothing, because n! It must not be equal to 0, so this detail can be ignored. 40*4. cyclic process: 41 * I = 2; 42 * j = 0; s = 2; f [0] = 2; c = 0; 43 * I = 3; 44 * j = 0; s = 6; f [0] = 6; c = 0; 45 * I = 4; 46 * j = 0; s = 24; f [0] = 4; c = 2; 47 * j = 1; s = 2; f [1] = 2; c = 0; 48 * I = 5; 49 * j = 0; s = 20; f [0] = 0; c = 2; 50 * j = 1; s = 12; f [1] = 2; c = 1; 51 * j = 2; s = 1; f [2] = 1; c = 0; 52 * loops until I = 30 ends. 53 */

 

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.