码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1018 Big Number

时间:2018-02-23 00:21:39      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:namespace   col   big   names   void   with   main   3.1   als   

Big Number

题意:算n!的位数。

题解:对于一个数来算位数我们一般都是用while去进行计算,但是n!这个数太大了,我们做不到先算出来在去用while算位数。

while(a){
    cnt++;
    a/=10;
}

 

将一个数对取10对数(取整),然后再加一就是这个数的位数,然后我们在算n!的时候每次对10取对数就好了。

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int main()
 5 {
 6     ios::sync_with_stdio(false);
 7     cin.tie(0);
 8     int t, n;
 9     cin >> t;
10     while(t--)
11     {
12         cin >> n;
13         double sum = 0.0;
14         for(int i = 1; i <= n; i++)
15             sum+=log10(i);
16         cout <<(int)sum+1<<endl;
17     }
18     return 0;
19 }

 

然后有一个公式叫做斯特林公式, 它可以算出n!的近似值所以我们也可以用斯特拉公式来求解。

 1 #include<cstdio>
 2 #include<cmath>
 3 using namespace std;
 4 #define Pi 3.14159265
 5 #define e 2.71828182
 6 int main(void)
 7 {
 8     int t, n;
 9     double ans;
10     scanf("%d",&t);
11     while(t--)
12     {   scanf("%d",&n);
13         ans = log10(sqrt(2*Pi*n))+n*log10(n/e);
14         printf("%d\n",(int)ans+1);
15     }
16     return 0;
17 }

 

HDU 1018 Big Number

标签:namespace   col   big   names   void   with   main   3.1   als   

原文地址:https://www.cnblogs.com/MingSD/p/8460540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!