본문 바로가기
Problem Solving/Baekjoon

[백준] 18268 Cow Gymnastics - Brute Force / Java

by graycode 2023. 10. 6.

 문제 링크

 

18268번: Cow Gymnastics

The consistent pairs of cows are $(1,4)$, $(2,4)$, $(3,4)$, and $(1,3)$.

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int k = read(), n = read(), t = k;

        int[] arr = new int[n];
        int[][] mat = new int[n][n];

        while (t-- > 0) {
            for (int i = 0; i < n; i++) arr[i] = read() - 1;

            for (int i = 0; i < n; i++)
                for (int j = i + 1; j < n; j++) mat[arr[i]][arr[j]]++;
        }

        int cnt = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) if (mat[i][j] == k) cnt++;

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글