Factorial (Large Number Multiplication Simulation Practice)

factorial


The factorial of the title description N is written as N! Represents the product of all positive integers less than or equal to N. The factorial will quickly become larger, such as 13! must be stored in a 32-bit integer type, 70! Even with floating point numbers, it can't be saved. Your task is to find the non-zero position at the end of the factorial. For example, 5!=1 2 3 4 5=120, so the last non-zero bit of 5! is 2, 7! =1 2 3 4 5 6 7=5040, so the last non-zero bit is 4.

Enter
a line with an integer N not greater than 4,220.

Output
a line, output N! the last non-zero bit.

Sample input
7

Sample output
4

Simple and crude multiplication of large numbers

#include<stdio.h>
int a[100000];
int main()
{
    
    
    int i,j,k,s,l=0,n;
    scanf("%d",&n);
    a[0]=1;
    //计算出前n个数的和,简单暴力。
    for(i=1;i<=n;i++)
    {
    
    
        int x=0,y=0;
        for(j=0;j<10000;j++)
        {
    
    
            x=a[j]*i+y;
            y=x/10;
            a[j]=x%10;
        }
    }
    for(i=0;;i++)//循环遍历第一个不为0的数就是计算出来的末尾不为0的第一个数!
    {
    
    
        if(a[i]!=0)
        {
    
    
            printf("%d\n",a[i]);
            break;
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/111419800