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
给你三个数x,y,z,比较这12个式子,问你哪个式子最大
0.1 ≤ x, y, z ≤ 200.0
x^(y^z)
这个式子log一下
变成
原式 = y^z*log(x)
再log一下变成
= log(y^z*log(x))
= log(y^z) + log(log(x))
= z * log(y) + log(log(x))
本来这样就可以比较了
可是题目的范围是0.1
log()小数会产生负数
log负数就没意义了
所以对于log(log(x))这么写不行
double范围 -1.7*10(-308)~1.7*10(308)
long double范围 128 18-19 -1.2*10(-4932)~1.2*10(4932)
虽然他们两精度都是16位,但是200的200次方long double竟然存的下
所以只要一次log就好了
然后愉快的写代码吧
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
char str[12][10] =
{
"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 x, y, z;
long double mx, t;
int pos;
void judge(int x)
{
if(fabs(mx - t) <= 1e-6) return ;
else if(mx < t)
{
pos = x;
mx = t;
}
}
int main()
{
cin >> x >> y >> z;
pos = 0;
mx = pow(y, z)*log(x);
t = pow(z, y)*log(x);
judge(1);
t = z*log(pow(x, y));
judge(2);
t = y*log(pow(x, z));
judge(3);
t = pow(x, z)*log(y);
judge(4);
t = pow(z, x)*log(y);
judge(5);
t = z*log(pow(y, x));
judge(6);
t = x*log(pow(y, z));
judge(7);
t = pow(x, y)*log(z);
judge(8);
t = pow(y, x)*log(z);
judge(9);
t = y*log(pow(z, x));
judge(10);
t = x*log(pow(z, y));
judge(11);
printf("%s\n", str[pos]);
}
其实log()一个负数是可以解的
还记得当年大明湖畔的欧拉公式吗
eiπ = -1
因为e的i∏次方等于-1
所以log(-1) = i∏
所以负数迎刃而解
log(-2) = log(-1 * 2) = log(-1) + log(2)
那log(i)呢
根号-1等于i
所以log(i) = log( -1^(1/2) ) = 1/2 * log(-1) = 1/2 * i∏
那log(a + bi)
欧拉原公式写作
eix = cosx + isinx
#include <iostream>
#include <complex>
#include <string>
using namespace std;
bool bigger (complex<long double> a, complex<long double> b)
{
if (imag(a) == 0 && imag(b) == 0)
{//没有虚部
return real(a) > real(b);//比较实部
}
else if (imag(a) == 0 && imag(b) != 0)
{ //有虚部的肯定小
return true;
}
else if (imag(a) != 0 && imag(b) == 0)
{
return false;
}
else if (imag(a) != 0 && imag(b) != 0)
{//都有虚部,按实部反过来比
return real(a) < real(b);
}
}
int main ()
{
long double ax, ay, az;
cin >> ax >> ay >> az;
complex<long double> x (ax, 0.0L);
complex<long double> y (ay, 0.0L);
complex<long double> z (az, 0.0L);
complex<long double> cmaz (3, 3);
string ans = "xd";
if (bigger(z * log(y) + log(log(x)), cmaz))
{
cmaz = z * log(y) + log(log(x));
ans = "x^y^z";
}
if (bigger(y * log(z) + log(log(x)), cmaz))
{
cmaz = y * log(z) + log(log(x));
ans = "x^z^y";
}
if (bigger(log(y * z) + log(log(x)), cmaz))
{
cmaz = log(y * z) + log(log(x));
ans = "(x^y)^z";
}
if (bigger(z * log(x) + log(log(y)), cmaz))
{
cmaz = z * log(x) + log(log(y));
ans = "y^x^z";
}
if (bigger(x * log(z) + log(log(y)), cmaz))
{
cmaz = x * log(z) + log(log(y));
ans = "y^z^x";
}
if (bigger(log(x * z) + log(log(y)), cmaz))
{
cmaz = log(x * z) + log(log(y));
ans = "(y^x)^z";
}
if (bigger(y * log(x) + log(log(z)), cmaz))
{
cmaz = y * log(x) + log(log(z));
ans = "z^x^y";
}
if (bigger(x * log(y) + log(log(z)), cmaz))
{
cmaz = x * log(y) + log(log(z));
ans = "z^y^x";
}
if (bigger(log(x * y) + log(log(z)), cmaz))
{
cmaz = log(x * y) + log(log(z));
ans = "(z^x)^y";
}
cout << ans << endl;
}