hdu_1018_Big Number_求阶乘长度模板题

本文介绍了一个计算大整数阶乘长度的问题,通过两种方法实现:一种是直接计算法,另一种是斯特林公式近似法。文章提供了一段C++代码示例,用于计算不同整数阶乘的位数。

阶乘长度模板!!

http://acm.hdu.edu.cn/showproblem.php?pid=1018

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12440    Accepted Submission(s): 5558


Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
 

Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 10 7 on each line.
 

Output
The output contains the number of digits in the factorial of the integers appearing in the input.
 

Sample Input
  
2 10 20
 

Sample Output
  
7 19
 

    1. #include<iostream>
    2. #include <cmath>
    3. using namespace std;
    4. int normal(double n)
    5. {
    6.     double x=0;
    7.     while(n)
    8.     {
    9.         x +=log10(n);
    10.         n--;
    11.     }
    12.     return (int)x+1;
    13. }
    14. long stirling(double n)
    15. {
    16.     long x=0;
    17.     if( n ==1 )
    18.         x = 1;
    19.     else
    20.     {
    21.         x = (long)( (log10(sqrt(4.0*acos(0.0)*n)) + n*(log10(n)-log10(exp(1.0)))) + 1 );
    22.     }
    23.     return x;
    24. }
    25. int main()
    26. {
    27.     int n;
    28.     cin>>n;
    29.     while(n--)
    30.     {
    31.         int x;
    32.         cin>>x;
    33.         cout<<stirling(x)<<endl;
    34.     }
    35.     return 0;
    36. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值