Large number n!

Source: Internet
Author: User

Usually solves the problem of large numbers of operation data out of range, overflow. Arrays are generally used to simulate. The n! can be thought of as the process of multiplying two integers at a time, so the process of multiplying by large numbers can be simulated. Just need to add some variables to store the values of the middle carry and the current bit.

#include <iostream>using namespace std; #define Max_wei 100000int result[max_wei];int wei_location;int up;int Main    () {int i,n;    Int J;        while (scanf ("%d", &n)!=eof) {memset (result,0,sizeof (result));        RESULT[1] = 1; Wei_location=1;        The number of bits of the current result, which facilitates multiplication and final output up = 0;            if (n==0 | | n==1) {printf ("1\n");        Continue } for (i=2;i<=n;i++)//n!  Each number in the {for (J =1,up =0;j<=wei_location;j++)//simulation multiplication step: Each is multiplied by the current number {Result[j] = Result[j] *i + up;                 Current number + front carry number up = RESULT[J]/10;  RESULT[J] =result[j]%10;  Save one per digit} while (up > 0) {result[j++] + = up%10;            Pay attention to jump out is J is Taichi up= UP/10; } wei_location= j-1;//Notice that the J is Taichi} for (i = wei_location;i>=1;i--) (i!=1)?    printf ("%d", Result[i]): printf ("%d\n", Result[i]); } return 0;}



Large number 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.