Codeforces #341 D. Rat Kwesh and Cheese 浮点数处理技巧 好题

题目

题目链接:http://codeforces.com/problemset/problem/621/D

题目来源:cf#341

简要题意:给定公式求值最大中下标最小的公式。

题解

这题需要对于浮点数有比较高深的理解。首先复习一下浮点数的范围。

类型位数有效数字数值范围
float326-7 ±3.4×1038
double6415-16 ±1.7×10308
long double12818-19 ±1.2×104932

考虑取一个对数的话,值就算用double也是不够的,要用long double。

然后将对应的公式取对数就行了。取pow的时候需要使用powl,这是long double版本的pow函数。

由于考察的知识比较偏门,所以说这道D过的人比E还要少,不过A了它的确可以学到新姿势。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
char out[12][15] = {
    "x^y^z",
    "x^z^y",
    "(x^y)^z",
    "(x^z)^y",
    "y^x^z",
    "y^z^x",
    "(y^x)^z",
    "(y^z)^x",
    "z^x^y",
    "z^y^x",
    "(z^x)^y",
    "(z^y)^x"
};

long double a[15];
int main() {
    double x, y, z;
    scanf("%lf%lf%lf", &x, &y, &z);
    long double logx = log(x);
    long double logy = log(y);
    long double logz = log(z);
    a[0] = logx * powl(y, z);
    a[1] = logx * powl(z, y);
    a[2] = logx * y * z;
    a[3] = -1;
    a[4] = logy * powl(x, z);
    a[5] = logy * powl(z, x);
    a[6] = logy * x * z;
    a[7] = -1;
    a[8] = logz * powl(x, y);
    a[9] = logz * powl(y, x);
    a[10] = logz * x * y;
    a[11] = -1;

    long double ans = a[0];
    int ind = 0;
    for (int i = 1; i < 12; i++) {
        if (!fabs(ans-a[i]) < 1e-8 && a[i] > ans) {
            ans = a[i];
            ind = i;
        }
    }
    printf("%s\n", out[ind]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值