본문 바로가기
학부공부/Java_Basic

Chapter 19. Generics[Java Basic]

by sonpang 2021. 11. 6.
반응형

19.A - 중복된 값을 가지지 않는 ArrayList

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

ArrayList를 입력받아 중복된 값을 가지지 않는 새로운 ArrayList를 return 하는 Method를 작성하시오. 

(Distinct elements in ArrayList) Write the following method that returns a new ArrayList. The new list contains the non-duplicate elements from the original list.

 

INPUT

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

* Line 2 : 자료의 개수 N (1~1,000 범위의 정수) 

* Line 3 ~ N+2 : 배열의 원소

 

OUTPUT

* Line 1 ~ ? : 중복이 제거된 배열의 원소

 

SAMPLE CODE

import java.util.ArrayList;
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();

        if (type.compareTo("String") == 0) {
            ArrayList<String> list1 = new ArrayList<String>();
            for (int i = 0; i < N; i++) list1.add(sc.next());
            for (String val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Integer") == 0) {
            ArrayList<Integer> list1 = new ArrayList<Integer>();
            for (int i = 0; i < N; i++) list1.add(sc.nextInt());
            for (int val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Double") == 0) {
            ArrayList<Double > list1 = new ArrayList<Double >();
            for (int i = 0; i < N; i++) list1.add(sc.nextDouble());
            for (double val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        }
    }

    YOUR_CODE
}

 

SAMPLE INPUT

Double 10 2.9 2.9 2.3 1.1 2.3 1.0 1.1 2.3 2.6 2.7

 

SAMPLE OUTPUT

2.9 2.3 1.1 1.0 2.6 2.7

 

HINT

ArrayList<E> removeDuplicates(ArrayList<E> list)

 

import java.util.ArrayList;
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();
 
        if (type.compareTo("String") == 0) {
            ArrayList<String> list1 = new ArrayList<String>();
            for (int i = 0; i < N; i++) list1.add(sc.next());
            for (String val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Integer") == 0) {
            ArrayList<Integer> list1 = new ArrayList<Integer>();
            for (int i = 0; i < N; i++) list1.add(sc.nextInt());
            for (int val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Double") == 0) {
            ArrayList<Double > list1 = new ArrayList<Double >();
            for (int i = 0; i < N; i++) list1.add(sc.nextDouble());
            for (double val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        }
    }
 
    public static <E extends Comparable<E>> ArrayList<E> removeDuplicates(ArrayList<E> l) {
        int i, j;
        for (i = 0; i < l.size() - 1; i++)
            for (j = i + 1; j < l.size(); j++)
                if (l.get(i).compareTo(l.get(j)) == 0)
                    l.remove(j);
        return l;
    }
}

 

 

19.B - Generic Max

Time Limit: 1s Memory Limit: 128MB

 

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)

 

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));
        }
    }
 
    public static <E extends Comparable<E>> E max(E[][] list) {
        E max = list[0][0];
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[i].length; j++) {
                if (list[i][j].compareTo(max) > 0)
                    max = list[i][j];
            }
        }
        return max;
    }
}
반응형

 

 

19.C - 복소수 행렬

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

Ex 13.17에서 소개된 Complex 클래스를 이용해 복소수 연산을 제공하는 복소수 행렬 클래스를 만들어라. 복소수 행렬 클래스는 GenericMatrix 클래스를 상속받아야 하며, add과 mutiple, zero 메소드를 구현해야 한다. Complex는 Number의 자식 클래스가 아니라서 GenericMatrix에서 Number를 Object로 변경할 필요가 있다. 

(ComplexMatrix) Use the Complex class introduced in Programming Exercise 13.17 to develop the ComplexMatrix class for performing matrix operations involving complex numbers. The ComplexMatrix class should extend the GenericMatrix class and implement the add, multiple, and zero methods. You need to modify GenericMatrix and replace every occurrence of Number by Object, because Complex is not a subtype of Number. Write a test program that creates the following two matrices and displays the result of addition and multiplication of the matrices by invoking the printResult method.

 

INPUT

* Line 1 : 행과 열의 개수 N (N은 1~9 범위의 홀수)

* Line 2 ~ N+1 : 행렬1 

* Line N+2 ~ 2N+1 : 행렬2

 - 행렬의 각 행은 "실수1 허수1 실수2 허수2 ~ 실수N 허수N" 형태로 구성되어 있음

 - 실수와 허수는 1 ~ 9 범위의 정수

 

OUTPUT

샘플 출력의 형식에 맞추어 출력한다

 

SAMPLE CODE

import java.util.ArrayList;
import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        // Create two Complex arrays m1 and m2
        Complex[][] m1 = new Complex[N][N];
        Complex[][] m2 = new Complex[N][N];
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                m1[i][j] = new Complex(sc.nextInt(), sc.nextInt());
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                m2[i][j] = new Complex(sc.nextInt(), sc.nextInt());

        // Create an instance of ComplexMatrix
        ComplexMatrix rationalMatrix = new ComplexMatrix();

        System.out.println("m1 + m2 is ");
        GenericMatrix.printResult(
                m1, m2, rationalMatrix.addMatrix(m1, m2), '+');

        System.out.println("m1 * m2 is ");
        GenericMatrix.printResult(
                m1, m2, rationalMatrix.multiplyMatrix(m1, m2), '*');
    }
}

class ComplexMatrix extends GenericMatrix<Complex> {
    @Override
    /** Add two rational numbers */
    protected Complex add(Complex r1, Complex r2) {
        return r1.add(r2);
    }

    @Override
    /** Multiply two rational numbers */
    protected Complex multiply(Complex r1, Complex r2) {
        return r1.multiply(r2);
    }

    @Override
    /** Specify zero for a Complex number */
    protected Complex zero() {
        return new Complex(0, 0);
    }
}

YOUR_CODE

 

SAMPLE INPUT

3

2 1 5 2 1 4 6 1 8 7 1 5 7 6 9 1 3 5 9 5 5 9 8 9 9 5 6 2 7 5 9 4 8 7 1 5

 

SAMPLE OUTPUT

m1 + m2 is 2.0+1.0i 5.0+2.0i 1.0+4.0i 9.0+5.0i 5.0+9.0i 8.0+9.0i 11.0+6.0i 10.0+11.0i 9.0+13.0i 6.0+1.0i 8.0+7.0i 1.0+5.0i + 9.0+5.0i 6.0+2.0i 7.0+5.0i = 15.0+6.0i 14.0+9.0i 8.0+10.0i 7.0+6.0i 9.0+1.0i 3.0+5.0i 9.0+4.0i 8.0+7.0i 1.0+5.0i 16.0+10.0i 17.0+8.0i 4.0+10.0i m1 * m2 is 2.0+1.0i 5.0+2.0i 1.0+4.0i 9.0+5.0i 5.0+9.0i 8.0+9.0i 41.0+102.0i 7.0+84.0i 13.0+74.0i 6.0+1.0i 8.0+7.0i 1.0+5.0i * 9.0+5.0i 6.0+2.0i 7.0+5.0i = 75.0+191.0i 28.0+164.0i 36.0+161.0i 7.0+6.0i 9.0+1.0i 3.0+5.0i 9.0+4.0i 8.0+7.0i 1.0+5.0i 116.0+200.0i 22.0+178.0i 38.0+183.0i

 

HINT

    @Override
    public String toString() {
        if (b >= 0)
            return a + "+" + b + "i";
        else
            return a + b + "i";
    }
    public Complex multiply(Complex secondComplex) {
        double newA = a * secondComplex.getA() - b * secondComplex.getB();
        double newB = b * secondComplex.getA() + a * secondComplex.getB();
        return new Complex(newA, newB);
    }

 

 

반응형

댓글