Easy Function题解——大数的大小比较(log函数取对数)

Input
Input contains multiple test cases. One line one case, and each case is three integers, a, b, c. We guarantee that 1 ≤ a, b, c ≤ 20000.(题意:比较a!和b^c的大小,相等则输出0,后者大则输出-1,前者大则输出1)

Output
For each case, print the function result, and one line one case.

Sample Input
1 1 1
3 2 3
5 11 2

Sample Output
0
-1
-1

题解:要比较a!和b^c的大小,但是数据范围很大,a的阶乘会爆,b的c次方也会爆,所以做这道题得有技巧,可以用对数函数log去缩小,因为等式两边同时取对数还是相等的
下面说说对数函数log:
注意:log函数的值是浮点型的!

	printf("%f\n",log(10)); //以e为底的对数函数 
	printf("%f\n",log10(100)); //以10为底的对数函数 
	printf("%f\n",log(8)/log(2)); //计算log2(8),运用换底公式 
	printf("%f\n",exp(1)); //计算自然常数e

在这里插入图片描述
运用下面两条公式即可去缩小a!和b^c的值:
1. log(a * b)=log(a)+log(b)
2. log(b^c)=c*log(b)

对 a!取对数即可表示为log(1*2 *3 *··· *(a-1)*a)=log(1)+log(2)+…+log(a-1)+log(a)
对b^c取对数即可表示为log(b ^ c)= c * log(b)

下面为该题AC代码:

#include<iostream>
#include<cmath>
using namespace std;
double q[20005];
int main()
{
    int a,b,c;
    for(int i=1;i<=20000;i++){
        q[i]=q[i-1]+log(i*1.0);
    }
    while(!(cin>>a>>b>>c).eof())
    {
        double t=0;
        t=1.0*c*log((double)b);
        if(t==q[a])
            cout<<0<<endl;
        else if(t>q[a])
            cout<<-1<<endl;
        else
            cout<<1<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值