Factorial of N (big integer multiplication) -- 51Nod, factorial -- 51nod

Source: Internet
Author: User

Factorial of N (big integer multiplication) -- 51Nod, factorial -- 51nod

Fortunately, I wrote an extra-large integer computation, and successfully compiled. (This is the implementation code of a big integer multiplication)

Let's put it simply. The idea of the big Integer Operation is to store numbers in arrays and simulate carry by arrays.

Of course, this uses one storage space. If you want to optimize the time, you can consider every four storage space.

#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <queue>#include <stack>using namespace std;int a[37000],k;void chengfa(int n){    int j,i,temp,carry;    a[0]=1;k=1;    for(i=2;i<=n;i++)    {        carry=0;        for(j=0;j<k;j++)            a[j]*=i;        for(j=0;j<k;j++)        {            temp=carry+a[j];            a[j]=temp%10;            carry=temp/10;        }        while(carry)        {            a[k++]=carry%10;            carry/=10;        }    }}int main(){    int n,flag;    while(~scanf("%d",&n))    {        memset(a,0,sizeof(a));        chengfa(n);        flag=0;        for(int i=k-1; i>=0; i--)        {            if(a[i]!=0||flag==1)            {                printf("%d",a[i]);                flag=1;            }        }        printf("\n");    }}

 

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.