[랜덤 마라톤] 쿠키의 신체 측정(20125)

lhs's avatar
Nov 20, 2024
[랜덤 마라톤] 쿠키의 신체 측정(20125)
notion image
notion image

1. 문제 풀이 아이디어

  • 머리를 찾아 심장의 위치를 찾고, 심장을 기준으로 계산하면 문제를 해결할 수 있다.

2. 나의 정답 코드

public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); boolean head = false; int lh = 0, rh = 0, b = 0, ll = 0, rl = 0; int[] heart = new int[2]; for (int i = 0; i < n; i++) { String line = bufferedReader.readLine(); for (int j = 0; j < n; j++) { if (line.charAt(j) != '*') continue; if (!head) { head = true; heart[0] = i + 1; heart[1] = j; continue; } if (i == heart[0] && j < heart[1]) { lh++; } else if (i == heart[0] && j > heart[1]) { rh++; } else if (i > heart[0] && j == heart[1]) { b++; } else if (i > heart[0] && j == heart[1] - 1) { ll++; } else if (i > heart[0] && j == heart[1] + 1) { rl++; } } } System.out.printf("%d %d\n%d %d %d %d %d\n", heart[0] + 1, heart[1] + 1, lh, rh, b, ll, rl); bufferedReader.close(); } }

3. 정리

  • boolean값으로 head를 정의한 후 첫 *이 들어왔을 때 그 위치를 기준으로 심장의 좌표를 구해 heart 배열에 저장한다.
  • heart 배열을 기준으로 if문을 사용하여 각 신체의 크기를 계산하여 문제를 푼다.
Share article

LHS's Study Space