4445 - 복소수 행렬

Time Limit: 1s Memory Limit: 128MB

Snippet Judge Submissions: 58 Solved: 19
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

힌트코드1

    @Override
    public String toString() {
        if (b >= 0)
            return a + "+" + b + "i";
        else
            return a + b + "i";
    }

힌트코드2

    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);
    }

 

Source

JAVA2015 PE19.11