728x90
반응형
2024.07.31기준 - 실버5
백준, BEAKJOON, BOJ, JAVA, 자바
풀이
이 문제는 입력받은 문자열에서 N이 들어왔을 때, N이 가르키는 이름을 출력하는 문제입니다.
1. 우선 이름과 그 사람이 받은 나쁜말을 한 아이의 인덱스를 저장하는 클래스를 생성했습니다.
// 이름과 나쁜 말을 쓴 사람의 인덱스를 저장하는 클래스.
public static class Member {
String name;
List<Integer> Nlist;
public Member(String n, Queue<Integer> qu) {
name = n;
Nlist = new LinkedList<>();
while (!qu.isEmpty()) {
Nlist.add(qu.poll());
}
}
}
2. 입력을 0으로 받으면 멈추도록 해줍니다.
while (!(s = br.readLine()).equals("0")) { // 입력을 0으로 받았을 때, 멈춘다.
...
}
3. 0이 입력되지 않았다면 그룹 번호를 출력해줍니다.
if (count > 1) { // 첫 번째 그룹을 제외하고 한 칸 씩 띄워준다.
sb.append("\n");
}
// 그룹 번호를 출력하면서 count를 동시에 올려준다.
sb.append("Group ").append(count++).append("\n");
4. 입력받은 정보를 list에 저장합니다.
list = new LinkedList<>();
n = Integer.parseInt(s);
for (int i = 0; i < n; i++) {
qu = new LinkedList<>();
st = new StringTokenizer(br.readLine());
name = st.nextToken();
for (int j = 1; j < n; j++) {
if (st.nextToken().equals("N")) {
qu.add(j);
}
}
list.add(new Member(name, qu));
}
5. 입력받은 정보를 바탕으로 나쁜말을 받은 아이를 찾은 뒤 나쁜말을 한 아이의 이름을 찾습니다.
for (int i = 0; i < n; i++) {
if (list.get(i).Nlist.size() > 0) {
check = false; // 나쁜 말이 있다면 false
find(i); // 나쁜 말을 한 아이의 이름을 찾는 함수 호출.
}
}
나쁜말을 한다면 나쁜말을 한 아이의 이름을 구하는 함수를 호출합니다.
// 나쁜 말을 한 아이의 이름을 찾는 함수.
private static void find(int index) {
String name = list.get(index).name;
int in;
for (int i = 0; i < list.get(index).Nlist.size(); i++) {
in = index - list.get(index).Nlist.get(i);
if (in < 0) { // 인덱스가 음수가 된다면
in += n;
}
sb.append(list.get(in).name).append(" was nasty about ").append(name).append("\n");
}
}
코드
package Main;
import java.io.*;
import java.util.*;
public class Main {
// 이름과 나쁜 말을 쓴 사람의 인덱스를 저장하는 클래스.
public static class Member {
String name;
List<Integer> Nlist;
public Member(String n, Queue<Integer> qu) {
name = n;
Nlist = new LinkedList<>();
while (!qu.isEmpty()) {
Nlist.add(qu.poll());
}
}
}
static int n;
static List<Member> list;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int count = 1; // 그룹 수
boolean check; // 나쁜 말이 있는지 확인하는 boolean 변수
String s, name;
Queue<Integer> qu; // 나쁜 말을 한 아이의 인덱스를 저장하는 큐
StringTokenizer st;
while (!(s = br.readLine()).equals("0")) { // 입력을 0으로 받았을 때, 멈춘다.
if (count > 1) { // 첫 번째 그룹을 제외하고 한 칸 씩 띄워준다.
sb.append("\n");
}
// 그룹 번호를 출력하면서 count를 동시에 올려준다.
sb.append("Group ").append(count++).append("\n");
check = true;
list = new LinkedList<>();
n = Integer.parseInt(s);
for (int i = 0; i < n; i++) {
qu = new LinkedList<>();
st = new StringTokenizer(br.readLine());
name = st.nextToken();
for (int j = 1; j < n; j++) {
if (st.nextToken().equals("N")) {
qu.add(j);
}
}
list.add(new Member(name, qu));
}
for (int i = 0; i < n; i++) {
if (list.get(i).Nlist.size() > 0) {
check = false; // 나쁜 말이 있다면 false
find(i); // 나쁜 말을 한 아이의 이름을 찾는 함수 호출.
}
}
if (check) { // 나쁜 말을 한 아이가 없다면
sb.append("Nobody was nasty\n");
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
// 나쁜 말을 한 아이의 이름을 찾는 함수.
private static void find(int index) {
String name = list.get(index).name;
int in;
for (int i = 0; i < list.get(index).Nlist.size(); i++) {
in = index - list.get(index).Nlist.get(i);
if (in < 0) { // 인덱스가 음수가 된다면
in += n;
}
sb.append(list.get(in).name).append(" was nasty about ").append(name).append("\n");
}
}
}
728x90
반응형
'코딩테스트 일기 (BAEKJOON)' 카테고리의 다른 글
BEAKJOON / 백준 - JAVA 10829번 이진수 변환 (0) | 2024.08.01 |
---|---|
BEAKJOON / 백준 - JAVA 14429번 배스킨라빈스 31 (0) | 2024.08.01 |
BAEKJOON / 백준 - JAVA 4108번 지뢰찾기 (0) | 2024.07.30 |
BAEKJOON / 백준 - JAVA 31432번 소수가 아닌 수 3 (0) | 2024.07.29 |
BEAKJOON / 백준 - JAVA 24482번 알고리즘 수업 - 깊이 우선 탐색 4 (0) | 2024.07.29 |