hdu1008

以n为底的log数,等于n进制的数的位数-1.(hdu1141有用到这里)

参考代码:http://blog.csdn.net/hondely/article/details/5777495

还有个斯特林数,目前只能是记公式。

http://baike.baidu.com/link?url=nr464QIJ5EsYFsQgM9jec2XyUzErMqh3XjD9h0PXXGm2kKDnMaFwTbXMoq26g40Q3Au2KVwTAt1lZhw6Oyar9q

关于斯特林公式的这种算法,一开始傻傻的直接按公式算,然后log10,结果wr了,估计是因为e的取值,根据参考链接将式子经过一定的转化,然后再求,避免了给出具体的e值这种情况,就可以ac了。

命运这种东西,生来就是要被踏于足下的,如果你还未有力量反抗它,只需怀着勇气等待。—《龙族》微笑

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;

int main(){
    int t;
    int n;
    double ans;

    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        ans=0;
        /*while(n--){//当n等于0时还会执行一次while当中的内容,所以会出错
            ans+=log10((double)n);//头文件是math.h.
        }*/

        while(n){
            ans+=log10((double)n);
            n--;
        }
        ans=ans+1;

        printf("%d\n",(int)ans);
    }
}

斯特林公式:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#define PI 3.14159

int main(){
    int t;
    int n;
    double ans;

    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);//1到10之间只有log2和log10,log也可以,log指的是以e为底的log数
        ans=(0.5*log(2*PI*n)+n*log(n)-n)/log(10)+1;

        printf("%d\n",(int)ans);
    }
}



posted @ 2015-09-23 19:09  buzhidaohahaha  阅读(284)  评论(0编辑  收藏  举报