首页 > 试题广场 >

数字比较

[编程题]数字比较
  • 热度指数:3068 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛很喜欢对数字进行比较,但是对于3 > 2这种非常睿智的比较不感兴趣。上了高中之后,学习了数字的幂,他十分喜欢这种数字表示方法,比如xy

由此,他想出了一种十分奇妙的数字比较方法,给出两个数字x和y,请你比较xy和yx的大小,如果前者大于后者,输出">",小于则输出"<",等于则输出"="。


输入描述:

两个数字x和y。

满足1 <= x,y <= 109



输出描述:
一个字符,">","<"或者"="。
示例1

输入

2 2

输出

=
示例2

输入

2 4

输出

=
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
 
int main(){
    ll x, y;
    cin >> x >> y;
    char result = '=';
    if(log(x) * y == log(y) * x){
        result = '=';
    }else{
        result = log(x) * y < log(y) * x ? '<': '>';
    }
    cout << result << endl;
    return 0;
}

发表于 2018-08-07 11:33:55 回复(0)
import math
x1,y1=input().split(" ")
x=int(x1)
y=int(y1)
if y*math.log(x)>x*math.log(y):
    print(">")
elif y*math.log(x)==x*math.log(y):
    print("=")
else:
    print("<")

发表于 2018-07-28 23:32:48 回复(0)

首先转化为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;
}
编辑于 2018-07-21 23:45:20 回复(0)
参考了楼上的解法才做出来的,比较指数问题,化为对数求解,左右同时取对数,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("=");
       }
       }
    }

发表于 2018-07-21 09:21:05 回复(1)
WAK头像 WAK

比较指数问题,化为对数求解,左右同时取对数,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;

}

发表于 2018-07-20 19:59:17 回复(3)
c++答案(比较double类型的两个数不能直接用==):
#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;
}

发表于 2018-07-25 16:24:43 回复(1)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        int y = in.nextInt();
        System.out.print((x==4&&y==2)||(x==2&&y==4)||(x==y)?"=":x>y?"<":">");
    }
}

发表于 2018-10-29 17:42:10 回复(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;
}
编辑于 2018-10-12 16:01:07 回复(1)
//取对数后再比较
import math
import sys
t =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('=')

编辑于 2018-08-25 16:47:03 回复(0)
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("=");
        }
    }
}


发表于 2018-08-08 17:39:45 回复(0)

仅在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]
发表于 2018-08-03 12:32:59 回复(0)
#include <stdio.h>

int POW(int x,int y)
{
    int a,b;
    b=1;
    for (a=1;a<=y;a++)
    {
        b=b*x;
        return b;
    }
}
int main(void)
{
    int x,y;
    scanf("%d %d",&x,&y);
    if (POW(x,y)>POW(y,x))
    {
        printf(">");
    }
    if (POW(x,y)<POW(y,x))
    {
        printf("<");
    }
    else
    {
        printf("=");
    }
    return 0;
}

发表于 2018-07-30 08:32:26 回复(0)
一般而言:指数大于底数的数较大,但是排除几种特殊情况x==y,x==2&&y==4,x==4&&y==2相等,x==1的情况显而易见,其他满足一般情况
发表于 2018-07-25 11:12:16 回复(0)
取对数比较,否则容易越界

#include<iostream>
#include<string>
#include<math.h>
 
usingnamespacestd;
 
intmain()
{
    doublex, y, xy, yx;
    cin >> x >> y;
 
    xy = y * log(x);
    yx = x * log(y);
 
    if(xy / yx == 1) cout << '='<< endl;
    elseif(xy / yx > 1) cout << '>'<< endl;
    elsecout << '<'<< endl;
 
    return0;
}
发表于 2018-07-23 16:01:30 回复(0)

热门推荐

通过挑战的用户