首页 > 试题广场 >

比大小

[编程题]比大小
  • 热度指数:15303 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}比较整数 ab 的大小。若 a < b,输出 “<”;若 a = b,输出 “=”;若 a > b,输出 “>”。

输入描述:
\hspace{15pt}在一行中输入两个整数 a, b1 \leqq a, b \leqq 10^4),用空格隔开。


输出描述:
\hspace{15pt}输出一个字符,表示比较结果,不包含引号。
示例1

输入

1 2

输出

<

说明

因为 1 < 2,所以输出 “<”。
示例2

输入

1 1

输出

=
示例3

输入

2 1

输出

>

备注:
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-06-03 优化题面文本与格式。
要不来点抽象的解法?
#include <stdio.h>

int main() {
    int a = 0, b = 0;
    char ch = 0;

    scanf("%d %d", &a, &b);

    /*
    * 若结果为 a < b,则(a < b) - (a > b) ==> 1 - 0 = 1
    * 若结果为 a > b,则(a < b) - (a > b) ==> 0 - 1 = -1
    * 若结果为 a = b,则(a < b) - (a > b) ==> 0 - 0 = 0
    */
    switch ((a < b) - (a > b))
    {
    case -1:
        ch = '>';
        break;
    case 0:
        ch = '=';
        break;
    case 1:
        ch = '<';
        break;
    }

    printf("%c", ch);

    return 0;
}


发表于 2025-08-06 17:18:00 回复(1)
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%c", a > b ? '>' : a < b ? '<' : '=');
    return 0;
}
发表于 2025-07-07 10:31:54 回复(2)
a,b=map(int,input().split())
p=all(1<=x<=10000 for x in[a,b])
p1=a<b
p2=a>b
if p:
    print('<'if p1 else'>'if p2 else'=')
else:
    print('请满足1≤a,b≤10000的正整数')

发表于 2025-10-27 16:57:55 回复(0)
a,b = map(int,input().split())
if 1 <= a and b <= 10000:
    if a < b:
        print("<")
    elif a == b:
        print("=")
    elif a > b:
        print(">")
else:
    print("输入不符合要求")
发表于 2025-07-24 16:02:14 回复(0)
a,b = map(int,input().split())
if a<b:
    print("<")
elif a==b:
    print("=")
else:
    print(">")
发表于 2025-12-08 11:39:25 回复(0)
#include <stdio.h>

int main() 
{
    int a, b;
    if (scanf("%d %d", &a, &b) != 2) return 0;
    if (a < 1 || a > 10000 || b < 1 || b > 10000) return 0;
    if (a < b)       printf("<\n");
    else if (a == b) printf("=\n");
    else             printf(">\n");

    return 0;
}
没想过可以这么写,大家可以讨论一下
发表于 2025-11-19 20:10:02 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //使用Scanner录入两个整数a,b
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        //利用三元操作符比较,并打印
        System.out.println(a>b?">":(a==b?"=":"<"));
    }
}
发表于 2025-10-31 15:58:21 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int a, b;
    cin >> a >> b;
    if(a < b) cout << "<" << endl;
    else if(a == b) cout << "=" << endl;
    else cout << ">" << endl;
    return 0;
}
发表于 2025-10-10 21:02:08 回复(0)
//我的好哥哥们,你们好
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d%d",&a,&b);
    a>b?printf(">"):(a<b?printf("<"):printf("="));//两个三目运算符嵌套
    return 0;
}
发表于 2025-09-28 16:39:17 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a,b;
    cin >> a >> b;
    a < b && cout << "<";
    a == b && cout << "=";
    a > b && cout << ">";
}
// 64 位输出请用 printf("%lld")
发表于 2025-09-12 11:25:39 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        if(1<=a && 1<=b && a<=10000 &&b<=10000 ){
            if(a<b){
                System.out.println("<");
            } else if(a==b){
                System.out.println("=");
            } else{
                System.out.println(">");
            }
        }
    }
}
发表于 2025-08-25 17:09:20 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            int a = in.nextInt();
            int b = in.nextInt();
            String is = "<";
            if(a==b) is="=";
            if(a>b) is=">";
            System.out.println(is);
    }
}

发表于 2025-08-13 14:54:39 回复(0)
public class Program {
    public static void Main() {
        string[] input = System.Console.ReadLine().Split();
        // 验证输入数量
        if (input.Length < 2) {
            System.Console.WriteLine("请输入两个整数,用空格分隔");
            return;
        }
        if (!int.TryParse(input[0], out int a)) {
            System.Console.WriteLine("第一个值不是有效的整数");
            return;
        }
        if (!int.TryParse(input[1], out int b)) {
            System.Console.WriteLine("第二个值不是有效的整数");
            return;
        }
        // 比较逻辑
        System.Console.WriteLine(a > b ? ">" : (a < b ? "<" : "="));

    }
}
发表于 2025-08-10 15:24:13 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b= in.nextInt();
           
            System.out.println((a>b) ? ">" : (b>a)? "<":"=");
        }
    }
}

发表于 2025-07-30 16:01:10 回复(0)
using System;
public class Program {
    public static void Main() {
        string[] input=Console.ReadLine().Split();
        double a = int.Parse(input[0]);
        double b = int.Parse(input[1]);
        if (a>=b)
        {
            if(a>b)
            {
                Console.WriteLine(">");
            }
            else
            {
                Console.WriteLine("=");
            }
        }
        else{
            Console.WriteLine("<");
        }
         
        }
    }
发表于 2025-07-26 12:13:10 回复(0)
using System;
public class Program {
    public static void Main() {
        string[] input = Console.ReadLine().Split();
        int a = int.Parse(input[0]);
        int b = int.Parse(input[1]);
        if(a>b)
        {
            Console.WriteLine(">");
        }else if(a<b)
        {
            Console.WriteLine("<");
        }else
        {
            Console.WriteLine("=");
        }
    }
}
发表于 2025-07-16 00:46:39 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a,b;
    cin >> a >> b;
    if(a<b)
    {
        cout<<"<";

    }else if(a==b)
    {
        cout<<"=";
    }
    else if(a>b)
    {
        cout<<">";
    }
}
发表于 2025-07-14 11:47:55 回复(0)
a, b = map(int, input().split())

if a < b:
    print("<")
elif a == b:
    print("=")
else:
    print(">")
发表于 2025-07-13 05:55:24 回复(0)
a,b=map(int,input().split())
if a==b:
    print('=')
else:
    if a<=b:
        print('<')
    else:
        print('>')
发表于 2025-07-04 21:44:53 回复(0)
#include <stdio.h>

int main() {
    int a,b;
    while(1){
        if(scanf("%d%d",&a,&b)!=2){
            while(getchar()!='\n');
            continue;
        }
        while(getchar()!='\n');
        if((a<1||a>10000)||(b<1||b>10000)){
            continue;
        }
        if(a<b){
            printf("<");
        }else if(a==b){
            printf("=");
        }else{
            printf(">");
        }
        break;
    }
    return 0;
}

发表于 2025-07-01 18:34:05 回复(0)