fzu1050 Number lengths(对数公式)

http://acm.fzu.edu.cn/problem.php?pid=1050

cmath头文件里有两种对数log()和log10(),一个是自然对数,一个是以10为底,

求n!的位数,根据对数公式log10(MN) = log10(M)+log10(N),又发现位数等于log10(MN)+1。

注意double强制转int和用floor函数有区别!!!

floor返回原类型,而int返回int型。而且当如-3.2时,floor返回-4,int返回-3

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath> 
 7 #define lson l, m, rt<<1
 8 #define rson m+1, r, rt<<1|1
 9 #define IO ios::sync_with_stdio(false);cin.tie(0);
10 #define INF 1e9
11 typedef long long ll;
12 using namespace std;
13 int n;
14 int main()
15 {
16     while(cin >> n){
17         double ans = 0;
18         for(int i = 1; i <= n; i++){
19             double tmp = log10(double(i));
20             ans += tmp;
21         }
22         cout << int(ans)+1 << endl;
23     }
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/Surprisezang/p/8877812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值