Basic exercises - factorial


title: basic exercises factorial
categories:

  • ACM
  • Large numbers
    tags:
  • Tarsus factorial
    date: 2020-03-12 09:46:49

Multiplied by the maximum number of bits n m bits is (m + 1) * n (n <m)

problem

Questions basic training factorial

By submitting this question

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

Enter a positive integer n- , output n- ! Value.
  Wherein the n- !. 1 = 2 . 3 * ... * n- .

Algorithm Description

n- ! may be large, and the limited range of the computer can be represented by an integer, the method requires the use of high-precision calculations. Using an array A to represent a large integer a , A [0] indicates a ones digit, A [. 1] represents a ten, and so on.
  To a multiplying an integer k becomes an array A of each element are multiplied by k , the corresponding note processing carry.
  First, a set to 1, then by 2, by 3, when multiplied to n , that is obtained n ! Value.

Input Format

Input contains a positive integer n- , n- <= 1000.

Output Format

The output of the n- ! Exact value.

Sample input

10

Sample Output

3628800

algorithm

The actual number of bits that multiplies each in addition to 10 to determine the number of bits of i, len number of bits to save the current results, (len + 1) * i is the number of bits in the maximum range of the new results, in order to update and record the results of

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<queue>
#define PI 3.14159265358979323
using namespace std;
int a[2600];
int main(){
	int n;
	cin>>n;
	memset(a,0,sizeof(a));
	a[1]=1;
	int jinwei=0;
	int len=1;
	for(int i=2;i<=n;i++)
	{
		int tlen=len,t=i;
		int wei=0;
		while(t!=0)
		{
			t/=10;
			wei++;
		}
		for(int j=1;j<=(len+1)+wei;j++)
		{
			a[j]=i*a[j]+jinwei;
			if(a[j]>=10)
			{
				jinwei=a[j]/10;
				a[j]=a[j]%10;
			}
			else
			jinwei=0;
			if(j>tlen&&a[j])
			tlen=j;
		}
		len=tlen;
	}
	while(len)
	cout<<a[len--];
	cout<<endl;
}
Published 43 original articles · won praise 1 · views 921

Guess you like

Origin blog.csdn.net/qq_43985303/article/details/104865528