1251 단어 나누기

sycho·2023년 11월 27일
0

baekjoon-analysis

목록 보기
5/20

문제

https://www.acmicpc.net/problem/1384

Silver V

코드 (Java)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(sc.nextLine());
        int grp = 1;

        while (num != 0) {
            String[] names = new String[num];
            char[][] messages = new char[num][num];
            for (int i = 0; i < num; ++i) {
                names[i] = sc.next();
                for (int j = 0; j < num; ++j) {
                    if (j == 0) {messages[i][j] = 'P';}
                    else {
                        if (i-j < 0) messages[i][i-j+messages.length] = sc.next().charAt(0);
                        else messages[i][i-j] = sc.next().charAt(0);
                    }
                }
                sc.nextLine();
            }

            System.out.printf("Group %d\n", grp);
            boolean was_nasty = false;
            for (int i = 0; i < num; ++i) {
                for (int j = 0; j < num; ++j) {
                    int idx;
                    if (i-j < 0) idx = i-j+messages.length;
                    else idx = i-j;
                    if (messages[i][idx] == 'N') {
                        System.out.printf("%s was nasty about %s\n", names[idx], names[i]);
                        was_nasty = true;
                    }
                }
            }

            if (!was_nasty) System.out.println("Nobody was nasty");
            System.out.printf("\n");
            num = Integer.parseInt(sc.nextLine());
            grp++;
        }

        sc.close();
    }
}

풀이

Java (그리고 C++)에서 Python의 negative index같은 것을 구현하려면, array의 length를 활용하도록 하자.

profile
안 흔하고 싶은 개발자. 관심 분야 : 임베디드/컴퓨터 시스템 및 아키텍처/웹/AI

0개의 댓글