由此,他想出了一种十分奇妙的数字比较方法,给出两个数字x和y,请你比较xy和yx的大小,如果前者大于后者,输出">",小于则输出"<",等于则输出"="。
由此,他想出了一种十分奇妙的数字比较方法,给出两个数字x和y,请你比较xy和yx的大小,如果前者大于后者,输出">",小于则输出"<",等于则输出"="。
两个数字x和y。
满足1 <= x,y <= 109
一个字符,">","<"或者"="。
2 2
=
2 4
=
首先转化为ylogx和xlogy比大小
其实就是logx/x与logy/y比大小
由于函数logx/x的形状,在e处取极值点,情况简单,所以单纯数字分析就可以输出结果,没有必要去算:
#include<iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int x, y, flag = 1;
cin >> x >> y;
if(x > y){
x += y;
y = x - y;
x -= y;
flag = 0;
}
if((x == 2 && y == 4) || x == y)
cout<<'='<<endl;
else if(x == 1 && flag == 1)
cout<<'<'<<endl;
else if(x == 1 && flag == 0)
cout<<'>'<<endl;
else if(x == 2 && y == 3 && flag == 1)
cout<<'<'<<endl;
else if(x == 2 && y == 3 && flag == 0)
cout<<'>'<<endl;
else if(x == 2 && flag == 1)
cout<<'>'<<endl;
else if(x == 2 && flag == 0)
cout<<'<'<<endl;
else if(flag == 1)
cout<<'>'<<endl;
else
cout<<'<'<<endl;
}
参考了楼上的解法才做出来的,比较指数问题,化为对数求解,左右同时取对数,ylogx和xlogy比大小。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a = in.nextDouble();
double b = in.nextDouble();
double suma =b*Math.log(a);
double sumb =a*Math.log(b);
if(suma>sumb) {
System.out.println(">");
}
else if(suma<sumb) {
System.out.println("<");
}
else if(suma==sumb) {
System.out.println("=");
}
}
}
比较指数问题,化为对数求解,左右同时取对数,ylogx和xlogy比大小。
答案:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double x,y;
while(cin>>x>>y){
double x1 = y*log(x);
double y1 = x*log(y);
if(x1==y1)
cout<<"="<<endl;
else if(x1>y1)
cout<<">"<<endl;
else
cout<<"<"<<endl;
}
system("pause");
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
double a=y*log(x);
double b=x*log(y);
if(a-b<0.000001 && a-b>-0.000001) cout<<"="<<endl;
else if(a-b>0.000001) cout<<">"<<endl;
else cout<<"<"<<endl;
return 0;
}
测试通过,总感觉不太严谨!!!!!
#include <iostream>usingnamespacestd;intmain(){longlongx = 0;longlongy = 0;cin >> x;cin >> y;if((x == y) || (x / y == y) || (y / x == x) ){cout << "=";return0;}cout << ((x>y)? "<": ">");return0;}
//取对数后再比较import mathimport syst =sys.stdin.readline().strip('\n')t =list(map(int,t.split()))x =float(t[0])y =float(t[1])res =(y/x)*(math.log(x)/math.log(y))if res > 1:print('>')elif res < 1:print('<')else:print('=')
import java.util.Scanner;
public class Main
{
public static void main(String [] args)
{
Scanner scan=new Scanner(System.in);
double x=scan.nextDouble();
double y=scan.nextDouble();
double m1=Math.pow(x,y);
double m2=Math.pow(y,x);
if(m1>m2)
{
System.out.println(">");
}else if(m1<m2)
{
System.out.println("<");
}else
{
System.out.println("=");
}
}
}
仅在x或y都<=4的时候进行计算,否则只比较x与y的大小,值越小其指数结果越大。
x, y = raw_input().split(' ')
x, y = int(x), int(y)
r = ['<','>','=']
if x == y:
print '='
else:
if x == 1 or y == 1:
print r[x > y]
if x > 4 or y > 4:
print r[x < y]
else:
if x ** y == y ** x:
print '='
else:
print r[x ** y > y ** x]