D. Rat Kwesh and Cheese
2 seconds
256 megabytes
Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.
Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers x, y and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:
- a1 = xyz;
- a2 = xzy;
- a3 = (xy)z;
- a4 = (xz)y;
- a5 = yxz;
- a6 = yzx;
- a7 = (yx)z;
- a8 = (yz)x;
- a9 = zxy;
- a10 = zyx;
- a11 = (zx)y;
- a12 = (zy)x.
Let m be the maximum of all the ai, and c be the smallest index (from 1 to 12) such that ac = m. Rat's goal is to find that c, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that ac.
The only line of the input contains three space-separated real numbers x, y and z (0.1 ≤ x, y, z ≤ 200.0). Each of x, y and z is given with exactly one digit after the decimal point.
Find the maximum value of expression among xyz, xzy, (xy)z, (xz)y, yxz, yzx, (yx)z, (yz)x, zxy, zyx, (zx)y, (zy)x and print the corresponding expression. If there are many maximums, print the one that comes first in the list.
xyz should be outputted as x^y^z (without brackets), and (xy)z should be outputted as (x^y)^z (quotes for clarity).
1.1 3.4 2.5
z^y^x
2.0 2.0 2.0
x^y^z
1.9 1.8 1.7
(x^y)^z
题意就是给定了12个式子去求其中的最大值,因为精度的原因,如果要是直接求答案肯定是错误的,所以需要扩大一下精度的范围,采用对数运算法则。代码如下:
#include <bits/stdc++.h> using namespace std; int n; long double x, y, z; int main(void){ cin >> x >> y >> z; long double logx = log(x); long double logy = log(y); long double logz = log(z); char str[11][20] ={"x^y^z", "x^z^y", "(x^y)^z", "", "y^x^z", "y^z^x", "(y^x)^z", " ", "z^x^y", "z^y^x", "(z^x)^y" }; long double num[11]; num[0] = logx * pow(y, z); num[1] = logx * pow(z, y); num[2] = logx * y * z; num[3] = num[2]; num[4] = logy * pow(x, z); num[5] = logy * pow(z, x); num[6] = logy * x * z; num[7] = num[6]; num[8] = logz * pow(x, y); num[9] = logz * pow(y, x); num[10] = logz * x * y; int pos = -1; long double maxx = -1; for(int i = 0; i < 11; i ++){ if(abs(num[i] - maxx) > 1e-12 && maxx < num[i]){ maxx = num[i]; pos = i; } } cout << str[pos] << endl; }
查看原文:http://chilumanxi.org/2016/02/01/codeforces-round-341-div-2-d-rat-kwesh-and-cheese/