zoukankan      html  css  js  c++  java
  • HDU 1018 Big Number 数学题解

    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 ≤ 107 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的数位能够使用 digits = log10(n)

    然后利用斯特林公式求出n!的近似值就能够利用log10来求得数位了。

    斯特林公式百度百科有,这里不反复了。

    float不能AC的时候,就使用double吧。

    #include <stdio.h>
    #include <math.h>
    const float PI = 3.14159265358979323846f;
    
    inline int getDigits(int n)
    {
    	float num = float(n);
    	int ans = (int)(0.5*log10(2.0*PI*num) + num*(log(num)-1)/log(10.0)) + 1;
    	return ans;
    }
    
    int main()
    {
    	int T, n;
    	scanf("%d", &T);
    	while (T--)
    	{
    		scanf("%d", &n);
    		printf("%d
    ", getDigits(n));
    	}
    	return 0;
    }



  • 相关阅读:
    利用观察者模式 进行不同页面的传值
    axios请求处理
    百度地图实现鼠标绘制图形并获取相关数据
    web前端支付功能
    各种好用插件汇总(持续更新...)
    记录iview表单校验的"坑"
    JavaScript字符串方法
    2020面试汇总
    JavaScript作用域
    JavaScript原型到原型链
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6994265.html
Copyright © 2011-2022 走看看