Algorithm chapter-the exact value of factorial

Source: Internet
Author: User

  source : "Algorithmic Competition Primer Classic" Example 5.2.2

  title : Input not more than 1000 positive integer n, output n!=1*2*3*...*n accurate results.

  Example input : 30

  Sample Output : 265252859812191058636308480000000

  analysis : To save the results, you need to analyze 1000! how big. It's not hard to figure out a calculator, 1000! Approximately equals 4*102567, so you can save it with an array of 3,000 elements buf. For convenience, we let f[0] Save the result of the bit, f[1] is 10 bits, f[2] is the hundred ... (Why do you want to show it in reverse order?) Because if you say it from the low order, once the rounding is up ... ), the n! can be completed at the same time only by simulating the hand count. You need to ignore the leading 0 in the output

  Source :

  

#include <stdio.h>#include<string.h>Const intMAXN = the;intBUF[MAXN];intn_factorial () {intI,j,n,s,c; scanf ("%d",&N); memset (BUF,0,sizeof(BUF));//put the array f 0buf[0]=1;  for(i=2; i<=n;i++)//cyclic multiplication I{C=0;  for(j=0; j<maxn;j++)//each of them is multiplied by I (analog hand calculation){s= Buf[j] * i +C; BUF[J]= s%Ten;//keep in that bitc = s/Ten;//rounding up a bit        }    }    /*Output Results*/     for(j=maxn-1; j>=0; j--)        if(Buf[j]) Break;//Ignore leading 0     for(i=j;i>=0; i--) printf ("%d", Buf[i]); printf ("\ n"); return 0;}

Algorithm chapter-the exact value of factorial

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.