2016 Java Chapter 19 (Week 15)

From: 2016-11-23 00:00:00 To: 2016-12-28 00:00:00 Now: 2024-11-21 22:08:19 Status: Public

B - Generic Max

Time Limit: 1s Memory Limit: 128MB

Snippet Judge Submissions: 39 Solved: 28
Description

2차 배열 안에서 가장 큰 값을 찾아내는 Generic 메소드를 작성하시오.

(Maximum element in a two-dimensional array) Write a generic method that returns the maximum element in a two-dimensional array.

 

Input

* Line 1 : 자료형 (String, Integer, Double 중 하나)

* Line 2 : 행의개수N 열의개수M (N, M은 1~1,000범위의 정수)

* Line 3 ~ N+2 : 공백으로 구분된 M개의 값

Output

* Line 1 : 가장 큰 값

Sample Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String type = sc.next();
        int N = sc.nextInt();
        int M = sc.nextInt();

        if (type.compareTo("String") == 0) {
            String[][] arr1 = new String[N][M];
            for (int i = 0; i < N; i++)
                for (int j = 0; j < M; j++)
                    arr1[i][j] = sc.next();
            System.out.println(max(arr1));
        } else if (type.compareTo("Integer") == 0) {
            Integer[][] arr1 = new Integer[N][M];
            for (int i = 0; i < N; i++)
                for (int j = 0; j < M; j++)
                    arr1[i][j] = sc.nextInt();
            System.out.println(max(arr1));
        } else if (type.compareTo("Double") == 0) {
            Double[][] arr1 = new Double[N][M];
            for (int i = 0; i < N; i++)
                for (int j = 0; j < M; j++)
                    arr1[i][j] = sc.nextDouble();
            System.out.println(max(arr1));
        }
    }

    YOUR_CODE
}
Sample Input
Integer
3 3
5 2 3
2 4 3
3 4 5
Sample Output
5
Hint

public static <E extends Comparable<E>> E max(E[][] list)

 

Source

JAVA2015 PE19.6