列表

详情


JAVA26. 实现接口

描述

已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。

输入描述

两个整数

输出描述

两个整数中的最大值

示例1

输入:

1 3

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 13ms, 内存消耗: 9448KB, 提交时间: 2022-04-29

import java.util.Scanner;
import java.math.*;
import java.io.*;

public class Main {
    public static void main(String[] args)throws IOException {
        Comparator comparator = new ComparatorImpl();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        System.out.println(comparator.max(x, y));
    }
}

interface Comparator {
    int max(int x, int y);
}

class ComparatorImpl implements Comparator {
    public int max(int x, int y) {
        return Math.max(x, y);
    }
}

Java 解法, 执行用时: 14ms, 内存消耗: 9564KB, 提交时间: 2022-05-20

import java.util.Scanner;
import java.math.*;
import java.io.*;
public class Main {

    public static void main(String[] args)throws IOException {
        Comparator comparator = new ComparatorImpl();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        System.out.println(comparator.max(x, y));
    }

}

interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}

//write your code here......

class ComparatorImpl implements Comparator {
    public int max(int x, int y) {
        return Math.max(x, y);
    }
}

Java 解法, 执行用时: 20ms, 内存消耗: 11136KB, 提交时间: 2022-04-28

import java.util.Scanner;
import java.math.*;
import java.io.*;

public class Main {

    public static void main(String[] args)throws IOException{
        Comparator comparator = new ComparatorImpl();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
            int x = Integer.parseInt(str[0]);
            int y = Integer.parseInt(str[1]);
            System.out.println(comparator.max(x, y));
        }
    }


interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}

class ComparatorImpl implements Comparator{
    @Override
    public int max(int x,int y){
        return Math.max(x,y);
    }
}

Java 解法, 执行用时: 26ms, 内存消耗: 10540KB, 提交时间: 2022-02-10

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Comparator comparator = new ComparatorImpl();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            System.out.println(comparator.max(x, y));
        }
    }

}

interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}

//write your code here......
class ComparatorImpl implements Comparator{
    private int x;
    private int y;
    
    public int max(int x,int y)
    {
        return (x>y)?x:y;
    }
}

Java 解法, 执行用时: 27ms, 内存消耗: 10664KB, 提交时间: 2022-02-08

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Comparator comparator = new ComparatorImpl();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            System.out.println(comparator.max(x, y));
        }
    }

}

interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}
class ComparatorImpl implements Comparator{
    @Override
    public int max(int x,int y){
        return x>y?x:y;
    }
}
//write your code here......

上一题