Tarsus thinking - Tarsus factorial (within 1000) (c ++ description)

First, a very good solution factorial (c description) I saw at the beginning of this article link: https: //blog.csdn.net/lisp1995/article/details/52403507

My solution is a reference to his ideas.

It is well known within the ordinary factorial 100 (or less), we can directly calculate the (simple iterative or recursive), but more than a certain long long int (64-bit) range, it will overflow, then we can not directly by defining variables to find, and then ask the number of the large numbers we should be called, when it comes to large numbers, we are not starting from the point of view of variables, but from the point of view of the array, using the most primitive manual calculation method, and the array element number of bits is used for each element to make a so capable of storing a larger range.

Note, here we first need to know the maximum number of digits factorial of the number of the required range, in order to apply for an array of space, I Baidu a bit: https: //baike.baidu.com/item/%E9%98% ? B6% E4% B9% 98 /4437932 fr = aladdin
formula applications where: n-= 10 ^ M -> LG n-= M -> LG1 + LG2 + LG3 + LG4 + ... + LGN = M!!
recycled directly evaluated on the median may know, I seek 1000! The median 2567.6 is 2568

emmm, well, I began to explain factorial method for finding

We must first apply spatial array,
we set a [0] as an initial value equal to 1, dig as bits, is 1, TEMP is a temporary value that is used to store on a product of the number of bits
, such
as a [0] = 1
beginning provided binary number num is 0
TEMP = 2 + num a [0] * = 2
a [0] = TEMP% 10 = 2
num = TEMP / 10 = 0 is clearly in because multiplied by 2 when no carry is
followed by a [0] * 3 similar to the above
followed by a [0] * 4
case temp = a [0] * 4 + num = 24 is 4 bits are ten 2 is
so
a [0] = temp% 10 = 4 a [1] to deposit 2 is num = temp / 10
just when we set the number of bits so just a num also holds the forward number, so add the outside loop obtained for a last num (num> 0) to the forward-save on
(which is a vertical analog multiplying
0. 6
X. 4
-
2. 4
. you can be simulated by calculating the pen)
followed by a [0] * 5, a [ 1] * 5 + num
i.e. 24x5 4x5 = 20 corresponds to the forward operation of the code 0 is 2 standard temp = a [0] {4 } x 5 + num ( number each time a new multiplier when num is initialized to 0) so temp = 20
Then a [0] = temp% 10 = 0 exactly in line with the standard number is 0 num = temp / 10 = 2
exactly in line 2 is the number of forward
and then is plus 4x5 2x5 = 10 before the intake 12 so that the number 2 temp = 12
it is stored in a standard 2 (12% 10) is a forward one
so with a [2] into a deposit on a number of
so in this case over the inverse outputs (a [2] a [1 ] a [0]) is not 120
... behind the calculations are similar to this

Reference Code

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 int a[2600];
 int temp,dig,n,num;//temp  是一个临时的值,用来存放的是每一位计算的结果 dig 是位数
 //num是上一位所向前进的数
 cin >> n; 
 a[0] = 1;//初始化个位的数为 1 
 dig = 1; //位数是 1(刚开始的时候)
 for(int i = 2;i <= n;++i )
 {
 	num = 0; //刚开始的时候 num的值是 0
 	//以位数来循环求值 dig是位数 刚开始的时候是1 
 	for(int j = 0;j < dig;++j)
 	{
 		/*temp 用来存放某个位上的乘积(模拟竖式乘法单独位数相乘)加上 上一位位数 求得所向下一位数(数学中称满10进1)进的数*/
 		temp = a[j]*i+num;  
 		a[j] = temp%10;  //记得满10进1的原则  也就是对10求余的数才是本位保留的数 是10的倍数的是要进位 
 		num = temp /10; //进位 
 	} 
 	// 检查num是否是大于0 如果大于0 则说明还得向前进位
 	while(num)
 	{
 		a[dig] = num%10;
 		num/=10;
 		++dig;
 	} 	
 }
 //高位是在后面的 逆回来读 
 for(int i = dig -1;i >= 0;--i)
 {
 	printf("%d",a[i]);  
 	
 	
 }
 return 0;
}
Published 19 original articles · won praise 3 · Views 3815

Guess you like

Origin blog.csdn.net/weixin_42792088/article/details/87116774