zoukankan      html  css  js  c++  java
  • POJ 1423:Big Number 求N的阶乘的长度 斯特林公式

    Big Number
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 27027   Accepted: 8626

    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 <= m <= 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

    题意是要求N!有多少位。

    因为有斯特林公式,所以求n!的位数即log10(n)=log10(sqrt(2*acos(-1.0)*n))+n*log10(n/exp(1.0));

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    int main()
    {
    	int test;
    	long long n;
    	
    	cin >> test;
    	while (test--)
    	{
    		cin >> n;
    		double re = log10(sqrt(2 * acos(-1.0)*n)) + n*log10(n / exp(1.0));
    		cout << (int)re + 1 << endl;
    	}
    	return 0;
    }
    



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Photoshop
    前端性能优化
    Angular Cli和npm、node.js命令
    Angular项目结构
    页面布局
    滚动条与height
    1.2 Angular入门
    前端的e2e测试
    Angular的部署
    jQuery插件开发的基本形式
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899581.html
Copyright © 2011-2022 走看看