NYOJ-69数的长度

来源:互联网 发布:txt全本免费小说软件 编辑:程序博客网 时间:2024/05/30 23:22

一:普通取对数求长度

#include <iostream>#include <stdio.h>#include <math.h>using namespace std;int main(){    int t;    cin>>t;    while(t--)    {   int n;        cin>>n;        double sum=0;        for(int i=1;i<=n;i++)        {            sum+=log10(i);        }        cout<<(int)sum+1<<endl;    }    return 0;}

二:斯特林近似

注意:1的时候特判

#include <iostream>#include <cmath>#include <stdio.h>typedef long long ll;double PI=acos(-1);using namespace std;int main(){    int t;     cin >> t;    while (t--)    {        ll n;          cin >> n;          if(n==1)          {              cout<<1<<endl;              continue;          }        ll res =  0.5 * log10(2.0 * PI * n) + n * log10(n * 1.0 /exp(1)) + 1;          cout << res <<  endl;    }    return 0;}


0 0
原创粉丝点击