zoukankan      html  css  js  c++  java
  • hdu_1018(斯大林公式/n!的位数)

    题意:求大数n!的位数。

    根据n! = (int)log(n!)+1

    方法1:

    log(n!) = log(1*2*3*...*n) = log1+log2+...+logn

    方法2:

    斯大林公式:

    n! = sqrt(2*PI*n)*(n/e)^n

    两侧取对数有

    log10(n!) = 1/2log(2*PI*n) + n*log(n/e)

    code1:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 using namespace std;
     5 int main()
     6 {
     7     int n;
     8     scanf("%d",&n);
     9     int t;
    10     while(n--)
    11     {
    12         scanf("%d",&t);
    13         double ans = 0;
    14         for(int i = 1; i <= t; i++){
    15             ans+=log10(i);
    16         }
    17         ans++;
    18         printf("%d
    ",(int)ans);
    19     }
    20     return 0;
    21 }
    View Code

    code2:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <math.h>
     4 #define e 2.71828182
     5 #define PI acos(-1)
     6 int main()
     7 {
     8     int t;
     9     int n;
    10     double w;             //斯特林数
    11     scanf("%d",&t);
    12     while(t--)
    13     {
    14         scanf("%d",&n);
    15         w=(1.0/2*log10(2*PI*n)+n*log10(n/e));
    16         printf("%d
    ",(int) w+1);              //记得+1,不能少。
    17     }
    18     return 0;
    19 }
    View Code
  • 相关阅读:
    js获取浏览器和屏幕的各种宽度高度
    闭包
    原型与原型链
    vuex
    微信小程序天使童装答辩
    vue脚手架本地开发跨域请求设置
    mvvm和mvc
    Vue 中 methods,computed, watch 的区别
    keep-alive
    YII2组件之GridView
  • 原文地址:https://www.cnblogs.com/shanyr/p/6667170.html
Copyright © 2011-2022 走看看