우리나라 고유의 윷놀이는 네 개의 윷짝을 던져서 배(0)와 등(1)이 나오는 숫자를 세어 도, 개, 걸, 윷, 모를 결정합니다. 네 개 윷짝을 던져서 나온 각 윷짝의 배 혹은 등 정보가 주어질 때 도(배 1개, 등 3개), 개(배 2개, 등 2개), 걸(배 3개, 등 1개), 윷(배 4개), 모(등 4개) 중 어떤 것인지를 결정하는 프로그램을 작성하세요.
arr | result |
---|---|
[0, 1, 0, 1] | 개 |
[1, 1, 1, 0] | 도 |
[0, 0, 1, 1] | 개 |
arr | result |
---|---|
[0, 1, 0, 0] | 걸 |
public class Main {
public String solution(int[] arr1) {
String answer = "";
int zeroCnt = 0;
for(int num : arr1) {
if (num == 0) {
zeroCnt++;
}
}
switch (zeroCnt) {
case 0:
answer = "모";
break;
case 1:
answer = "도";
break;
case 2:
answer = "개";
break;
case 3:
answer = "걸";
break;
case 4:
answer = "윳";
break;
}
return answer;
}
}
같은 조 다른분의 코드. 어차피 0과 1뿐이니 더해도 되겠네!
public class Main {
public String solution(int[] arr1) {
int count = 0;
for(int i = 0; i < arr1.length; i++) {
count += arr1[i];
}
switch (count) {
case 0:
return "윷";
case 1:
return "걸";
case 2:
return "개";
case 3:
return "도";
default:
return "모";
}
}
}
첫째 줄에 정수 n이 주어진다. (0≤n≤100)
다음 예제와 같이 삼각형 모양으로 *
을 출력하세요.
(공백의 개수와 별의 개수를 정확하게 확인해주시길 바랍니다. 🙆🏻♂️)
star | result |
---|---|
3 | ![]() |
6 | ![]() |
star | result |
---|---|
9 |
public class Main {
public void solution(int star) {
for (int i = 0; i < star; i++) {
for (int j = 0; j < star - 1 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i * 2 + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
같은 조 다른분의 코드. 별찍기에 repeat()
을 쓸 생각은 못해봤네...신박!
public class Main {
public void solution(int star) {
for (int i = 0; i < star ; i++) {
System.out.println((" ").repeat(star - 1 - i) + ("*").repeat(i * 2 + 1));
}
}
}
5x5 2차원 배열이 주어질 때 어떤 원소가 상하좌우에 있는 원소보다 클 때 해당 위치에 * 을 표시하는 프로그램을 작성하세요. 경계선에 있는 수는 상하좌우 중 존재하는 원소만을 비교합니다.
array | result |
---|---|
[[3,4,1,4,9],[2,9,4,5,8],[9,0,8,2,1],[7,0,2,8,4],[2,7,2,1,4]] | 3 4 1 4 * 2 * 4 5 8 * 0 * 2 1 7 0 2 * 4 2 * 2 1 4 |
array | result |
---|---|
[[7,4,6,5,9], [6,1,3,4,5], [4,8,5,6,9], [1,3,0,6,4], [6,4,8,1,7]] | * 4 * 5 * 6 1 3 4 5 4 * 5 6 * 1 3 0 6 4 * 4 * 1 * |
이 문제는 2시간 정도 붙잡고 있었는데 결국 못 풀었다.
사실 제대로 자료구조니 알고리즘이니 공부 많이 못했는데, 아직 구현에 필요한 지식이 없는 것일 수도...
주의! 실패코드 입니다.
public void solution(int[][] arr1) {
String[][] answer = new String[5][5];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[i].length; j++) {
if (i < arr1.length - 1 && j < arr1[i].length - 1) {
if (arr1[i][j] > arr1[i + 1][j] && arr1[i][j] > arr1[i][j + 1]) {
answer[i][j] = "*";
} else {
answer[i][j] = String.valueOf(arr1[i][j]);
}
} else {
answer[i][j] = String.valueOf(arr1[i][j]);
}
}
}
for (int i = arr1.length - 1; i < arr1.length; i++) {
for (int j = arr1[i].length - 1; j < arr1[i].length; j++) {
if (arr1[i][j] > arr1[i - 1][j] && arr1[i][j] > arr1[i][j - 1]) {
answer[i][j] = "*";
}
}
}
for (int i = 0; i < answer.length; i++) {
for (int j = 0; j < answer[i].length; j++) {
System.out.print(answer[i][j] + " ");
}
System.out.println();
}
}