import java.util.Scanner;

public class CompareLinearList {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 输入A表的元素个数
        System.out.print("请输入A表的元素个数:");
        int m = scanner.nextInt();
        int[] A = new int[m];

        // 输入A表的元素
        System.out.print("请输入A表的元素:");
        for (int i = 0; i < m; i++) {
            A[i] = scanner.nextInt();
        }

        // 输入B表的元素个数
        System.out.print("请输入B表的元素个数:");
        int n = scanner.nextInt();
        int[] B = new int[n];

        // 输入B表的元素
        System.out.print("请输入B表的元素:");
        for (int i = 0; i < n; i++) {
            B[i] = scanner.nextInt();
        }

        // 比较A、B两表的大小
        int i = 0, j = 0;
        while (i < m && j < n) {
            if (A[i] < B[j]) {
                System.out.println("A < B");
                return;
            } else if (A[i] > B[j]) {
                System.out.println("A > B");
                return;
            }
            i++;
            j++;
        }

        if (i == m && j == n) {
            System.out.println("A = B");
        } else if (i == m) {
            System.out.println("A < B");
        } else {
            System.out.println("A > B");
        }
    }
}

使用示例: 请输入A表的元素个数:5 请输入A表的元素:1 2 3 4 5 请输入B表的元素个数:5 请输入B表的元素:1 2 3 4 5 A = B

Java线性表比较:比较两个int数组的大小

原文地址: https://www.cveoy.top/t/topic/puny 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录