The exact value of the factorial of the "algorithmic race get started classic"

Source: Internet
Author: User

The exact value of the factorial of the "algorithmic race get started classic"

Problem description

Enter a positive integer of not more than 1000 N, output n! = 1x2x3x4x Accurate results of Xn.

Example input: 5
Sample output: 120

Algorithm analysis

We need to address two questions:

  • How to save Results
  • What is the multiplication process?

  • For the first question: because the factorial of 1000 is not saved with an integer, as with double, it will be more than 2000 bits, save the number of bits of the factorial value, we can only use an array, where a size of 3000 is used;
    For the second question: we can simulate the mathematical multiplication process, take 1 as the base number, and deposit the array, starting with 2, then multiplying each bit in the array of the existing base number, setting a carry identifier, and then storing the result in the array;
    The first bit of storage, followed by 10 bits, hundred, thousands, and so on, the final result from the back to the output, the output needs to be the back of the excess 0 removed. (from the single-bit can also avoid meaningless 0 and a meaningful mix of 0, such as 5 of the factorial of 120, there is an array of 0, 1, 2, if it is in accordance with 120, the last 0 will be confused with the remaining 2,997 0, increasing the difficulty of processing)
    Code implementation
    #include <stdio.h>#include <string.h>#include <stdlib.h>Const intMAXN = the;intF[MAXN];intMain () {intI, J, N; while(scanf_s ("%d", &n)) {memset(F,0,sizeof(f)); f[0] =1; for(i =2; I <= N; i++) {//Multiply I            intc =0;//c is rounding .             for(j =0; J < Maxn; J + +) {ints = f[j] * i + C;//s is 3,000 bits multiplied by I plus carryF[J] = s%Ten;//Current bit storage bitsc = S/Ten;//Carry ID}        } for(j = maxn-1; J >=0; j--) {if(F[j]) Break;//From back to front, in turn remove the number of bits of 0}//Output Results         for(i = j; I >=0; i--)printf("%d", F[i]);printf("\ n"); } System ("Pause");return 0;}

    The exact value of the factorial of the "algorithmic race get started classic"

    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.