Gym-101502H Eyad and Math

本文介绍了一种通过取对数来比较两个指数表达式大小的方法,并提供了完整的C语言实现代码。这种方法避免了直接计算大指数可能引发的溢出问题。

Gym-101502H Eyad and Math


题目链接

题意:
判断a^b与c^d的大小,(1a,b,c,d ≤ 10^9)。

做法:
取对数,log(b)a^n = n*log(b)a
#include <stdio.h>
#include <math.h>

int
main() {
    int t, a, b, c, d;
    double e, f;

    scanf("%d", &t);
    while( t-- ) {
        scanf("%d %d %d %d", &a, &b, &c, &d);
        e = b * log(a);
        f = d * log(c);
        if( e < f ) {
            printf("<\n");
        }
        else {
            printf(">\n");
        }
    }
    return 0;
}
/*
反省:
下面是我开始的代码,因为指数太大,我开始的想法是取余然后比较大小,真的是傻。

举个例子:8 mod 5 == 8 * 6 mod 5,很容易就误判了。
*/

#include <stdio.h>

#define mod 1000000000

int  // 幂取模
pow_mod(int a, int n, int m) {
    if( n == 0 ) {
        return 1;
    }
    int x = pow_mod(a, n / 2, m);
    long long ans = (long long)x * x % m;

    if( n % 2 == 1 ) {
        ans = ans * a % m;
    }
    return (int)ans;
}

int
main() {
    int t, a, b, c, d, e, f;

    scanf("%d", &t);
    while( t-- ) {
        scanf("%d %d %d %d", &a, &b, &c, &d);
        e = pow_mod(a, b, mod);
        f = pow_mod(c, d, mod);
        if( e < f ) {
            printf("<\n");
        }
        else {
            printf(">\n");
        }
    }

    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值