Factorial calculation (high precision, short code)

Submit link: Blue Bridge Cup Practice System

topic:

Resource limit
Time limit: 1.0s Memory limit: 512.0MB

Problem description
  Input a positive integer n, and output the value of n!.
  Where n!=1 2 3*...*n.
  
The algorithm description
  n! may be very large, and the range of integers that the computer can represent is limited, and high-precision calculation methods are required. Use an array A to represent a large integer a, A[0] represents the ones digit of a, A[1] represents the tens digit of a, and so on.
  Multiplying a by an integer k becomes multiplying each element of array A by k. Please pay attention to the corresponding carry.
  First set a to 1, then multiply by 2, and multiply by 3. When multiplied to n, the value of n! is obtained.
  
Input format
  input contains a positive integer n, n<=1000.
  
Output format
  outputs the exact value of n!.
  
Sample input
10

Sample output
3628800

Problem-solving ideas:

Simulate the process of multiplication.

Note : The multiplied number will not be split anymore, only the result will be split, it is impossible to burst the data with 1 digit*1000

Insert picture description here

Code:

#include<stdio.h>
#include<string.h>
int a[10100];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		int i,j;
		int jinwei;//进位 
		int wnum;//位数
		a[1]=1;
		wnum=1;
		for(i=2;i<=n;i++)
		{
			jinwei=0;
			for(j=1;j<=wnum;j++)
			{
				a[j]=a[j]*i+jinwei;
				jinwei=a[j]/10;
				a[j]=a[j]%10;
			}
			while(jinwei>0)
			{
				a[j]=jinwei%10;
				jinwei/=10;
				j++;
			}
			wnum=j-1;//因为是j++的缘故,所以实际位数需要减1 
		}
		for(i=wnum;i>=2;i--)
			printf("%d",a[i]);
		printf("%d\n",a[i]); 
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/114647859