문제
- 백준 20125 (쿠키의 신체 측정) - Java
- 백준 문제를 풀던 도중, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5" 라는 메시지와 함께 에러가 났다.
시도
해결
오늘 푼 문제
- 백준 9655 (돌게임) -Java
- 돌 게임은 두 명이서 즐기는 재밌는 게임이다.
탁자 위에 돌 N개가 있다. 상근이와 창영이는 턴을 번갈아가면서 돌을 가져가며, 돌은 1개 또는 3개 가져갈 수 있다. 마지막 돌을 가져가는 사람이 게임을 이기게 된다.
두 사람이 완벽하게 게임을 했을 때, 이기는 사람을 구하는 프로그램을 작성하시오. 게임은 상근이가 먼저 시작한다. 상근이가 게임을 이기면 SK를, 창영이가 게임을 이기면 CY을 출력한다.
- 돌의 갯수 N을 2로 나누었을 때에 나머지가 1이면 SK, 나머지가 0이면 CY를 출력하면 된다.
import java.io.*;
public class Main {
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 num = Integer.parseInt(br.readLine());
if (num % 2 == 0) bw.write("CY");
else bw.write("SK");
bw.flush();
bw.close();
br.close();
}
}
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
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 num = Integer.parseInt(br.readLine());
for (int i = 1; i <= num; i++) {
int count = 0;
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int index = Integer.parseInt(st.nextToken());
List<Integer> arr = new ArrayList<>();
for (int k = 0; k < 20; k++) {
int input = Integer.parseInt(st.nextToken());
for (int j = 0; j < arr.size(); j++) {
if (arr.get(j) > input) {
count++;
}
}
arr.add(input);
}
bw.write(index + " " + count + "\n");
}
bw.flush();
bw.close();
br.close();
}
}
- 백준 8979 (올림픽) - Java
- https://www.acmicpc.net/problem/8979
- 처음에는 각 나라 별로 리스트를 만들어서 일일히 비교해야하나 생각했는데, 그렇게 하다보면 너무 비교할 것이 많고 복잡할 것 같았다.
- 그래서 그냥 금메달, 은메달, 동메달 별로 리스트를 만들고 각 리스트의 인덱스 값을 지정된 나라로 해서 비교하면 되지 않을까하고 구현해 보았다.
- 이렇게 하면 어차피 같은 등수의 나라가 있어도 비교하는 것에서는 상관 없을 것으로 판단하였다.
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int test = Integer.parseInt(st.nextToken());
int wantCountry = Integer.parseInt(st.nextToken());
int[] gold = new int[test];
int[] silver = new int[test];
int[] bronze = new int[test];
for (int i = 0; i < test; i++) {
st = new StringTokenizer(br.readLine(), " ");
int country = Integer.parseInt(st.nextToken());
gold[country - 1] = Integer.parseInt(st.nextToken());
silver[country - 1] = Integer.parseInt(st.nextToken());
bronze[country - 1] = Integer.parseInt(st.nextToken());
}
int res = 1;
for (int i = 0; i < test; i++) {
if (i == wantCountry - 1) continue;
if (gold[i] > gold[wantCountry - 1]) {
res++;
}
else if (gold[i] == gold[wantCountry - 1]) {
if (silver[i] > silver[wantCountry - 1]) {
res++;
}
else if (silver[i] == silver[wantCountry - 1]) {
if (bronze[i] > bronze[wantCountry - 1]) {
res++;
}
}
}
}
bw.write(String.valueOf(res));
bw.flush();
bw.close();
br.close();
}
}
- 백준 25757 (임스와 함께하는 미니게임) - Java
- set으로 사람들을 중복되지 않게 입력해주면 될 것으로 보인다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int test = Integer.parseInt(st.nextToken());
String game = st.nextToken();
Set<String> set = new HashSet<>();
for (int i = 0; i < test; i++) {
set.add(br.readLine());
}
if (game.equals("Y")) bw.write(String.valueOf(set.size()));
else if (game.equals("F")) bw.write(String.valueOf(set.size() / 2));
else if (game.equals("O")) bw.write(String.valueOf(set.size() / 3));
bw.flush();
bw.close();
br.close();
}
}
- 백준 20125 (쿠키의 신체 측정) - Java
import java.io.*;
import java.util.*;
public class Main {
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 num = Integer.parseInt(br.readLine());
char[][] arr = new char[num][num];
for (int i = 0; i < num; i++) {
String input = br.readLine();
for (int j = 0; j < num; j++) {
arr[i][j] = input.charAt(j);
}
}
int x = 0, y = 0;
for (int i = 0; i < num; i++) {
boolean flag = false;
for (int j = 0; j < num; j++) {
if (arr[i][j] == '*') {
y = i + 2;
x = j + 1;
flag = true;
break;
}
}
if (flag == true) break;
}
int[] size = new int[5];
for (int i = 0; i < x - 1; i++) {
if (arr[y - 1][i] == '*') size[0]++;
}
for (int i = x; i < num; i++) {
if (arr[y - 1][i] == '*') size[1]++;
}
for (int i = y; i < num; i++) {
if (arr[i][x - 1] == '*') size[2]++;
}
for (int i = y; i < num; i++) {
if (arr[i][x - 2] == '*') size[3]++;
}
for (int i = y; i < num; i++) {
if (arr[i][x] == '*') size[4]++;
}
bw.write(y + " " + x + "\n");
bw.write(size[0] + " " + size[1] + " " + size[2] + " " + size[3] + " " + size[4]);
bw.flush();
bw.close();
br.close();
}
}