Big Number大数目 16

本文介绍了一种计算大整数阶乘位数的方法,通过数学公式简化计算过程,避免了直接计算阶乘带来的巨大数值问题。提供了两种解题思路及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Big Number大数目

Problem Description

In manyapplications very large integers numbers are required. Some of theseapplications are using keys for secure transmission of data, encryption, etc.In this problem you are given a number, you have to determine the number ofdigits in the factorial of the number.

 在许多应用中非常大的整数数字是必需的。这些应用使用密钥数据,加密等安全传输在这个问题中,你将得到一个号码,则必须确定在数的阶乘的位数。

 

Input

Input consists ofseveral lines of integer numbers. The first line contains an integer n, whichis the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤107 on each line.

 输入由若干行整数数字。第一行包含一个整数n,这是要测试的情况下的数量,后跟n线路,一个整数1≤N≤107上的每一行。

Output

The outputcontains the number of digits in the factorial of the integers appearing in theinput.

 

输出包含的位数出现在输入的整数的阶乘的数目。

Sample Input

2

10

20

 

Sample Output

7

19

代码如下:

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#include<stdio.h>  
#include<math.h>  
//这个题的意思是:给你一个数,让你求出N!由多少位数构成,比如输出10,它的阶乖是3628800 由7位数构成,这时你要输出7;
//解题思路:
//1.可以暴力,N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N);
//2.Stirling公式:n!与√(2πn) * n^n * e^(-n)的值十分接近
//故log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(n);
//
//解法一:
//LANGUAGE:C++
//CODE:
double reback(int n)  //计算阶乘的位数 
{  
	int i;
    double cnt=0;  
    for(i=2;i<=n;i++)  
    {  
        cnt+=log10(i);  //把求阶乘转化成加法运算 
    }  
    return cnt;  
}  
  
int main()  
{  
    int group,n;  
    scanf("%d",&group);  
    while(group--)  
    {  
        scanf("%d",&n);  
        printf("%d\n",(int)reback(n)+1);  
    }  
    return 0;  
}  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值