이번에는 항해 기간중에 진행된 알고리즘 시험(테스트) 이다.
3번 문제는 풀 수는 있었지만 너무 하드코딩인거 같은데 다른 방법이 있는지는 지금 당장은 모르겠다...
단, 2번문제의 경우에는 처음부터 접근을 잘못해서 너무 어렵게 풀어버렸다. 그래서 다른 조원분이 하신 코드를 추가로 올린다.
public class Test04 {
// 우리나라 고유의 윷놀이는 네 개의 윷짝을 던져서 **배(0)와 등(1)**이 나오는 숫자를 세어
// 도, 개, 걸, 윷, 모를 결정합니다. 네 개 윷짝을 던져서 나온 각 윷짝의 배 혹은 등 정보가 주어질 때
// **도(배 1개, 등 3개), 개(배 2개, 등 2개), 걸(배 3개, 등 1개), 윷(배 4개), 모(등 4개)** 중
// 어떤 것인지를 결정하는 프로그램을 작성하세요.
public String solution(int[] arr1) {
// [0,1,0,1] : 개 0 2개
// [1,1,1,0] : 도 0 1개
// [0,0,1,1] : 개 0 2개
// [0,1,0,0] : 걸 0 3개
// 도 : 1 3개 , 0 1개
// 반환해줄 빈 문자열을 만들어준다.
String answer = "";
// 윷의 결과를 구할 count 변수를 만들어준다.
int count = 0;
// 반복문을 배열의 길이만큼 돌려준다.
// 배열안에서 0이 나올 때 마다 카운트를 증가시켜준다.
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] == 0)
count++;
}
// 스위치 문을 통해서 0 의 갯수에 따라서 도~모 까지
// answer 문자열에 값을 바꿔준다.
switch (count) {
case 1:
answer = "도";
break;
case 2:
answer = "개";
break;
case 3:
answer = "걸";
break;
case 4:
answer = "윷";
break;
default:
answer = "모";
}
// 최종적으로 리턴해준다.
return answer;
}
public static void main(String[] args) {
Test04 method = new Test04();
int[] arr1 = {1, 0, 1, 0};
System.out.println(method.solution(arr1));
}
}
public class Test05 {
// 첫째 줄에 정수 n이 주어진다. (0≤n≤100)
// 출력
// 다음 예제와 같이 삼각형 모양으로 ‘*’을 출력하세요.
// (공백의 개수와 별의 개수를 정확하게 확인해주시길 바랍니다. 🙆🏻♂️)
public void solution(int star) {
// 코드 작성
String startxt = "*"; // 별을 담고있는 문자열 담고있는 변수
String space = " "; // 빈 문자를 담고있는 문자열 변수
int count1 = star; // count1 에는 별을 star 를 가져온 값 넣어준다.
int count2 = 1; // count2 에는 1을 기본으로 담아준다.
// 반복문은 star 값 만큼 반복시켜 준다.
// count1 은 공백 값을 구하기 위해서 star 에서 -1 을 누적시켜서 빼준다.
// 프린트문 출력은 여백을 추가하고 이후 별도 추가해 준다. (피라미드 형태를 위해서)
// 이 후에는 카운트를 2씩 증가시켜준다. 이를 통해 별의 양쪽으로 피라미드 처럼 쌓인다.
for (int i = 1; i <= star; i++) {
count1 -= 1;
System.out.print(space.repeat(count1) + startxt.repeat(count2) + "\n");
count2 += 2;
}
}
public static void main(String[] args) {
Test05 method = new Test05();
int star = 6;
method.solution(star);
}
}
package Test2;
public class Main {
public void solution(int star) {
//세로 길이가 star인 별트리를 만들기
//세로 길이만큼 반복하면서 이중 for문을 통해 공백과 별을 각 줄에 해당 길이만큼 찍어주기.
//공백은 왼쪽에서 star길이만큼 띄우고 시작하여 줄이 바뀔때마다 1개씩 줄어든다.
//별은 한줄에 2개씩 늘어나고 마지막줄 별의 개수는 2*star-1이다.
for(int i = 1; i <= star; i++ ){ //바깥 for문으로 세로길이 숫자만큼 반복문을 돌려준다.
for(int j = star; j > i; j--){
System.out.print(" "); // 왼쪽 공백을 for문을 돌면서 줄이 바뀔때마다 star크기에서 하나씩 줄여준다.
}
for(int j = 1; j <= 2 * i -1; j++){
System.out.print("*"); //별은 1부터 시작해서 줄이 바뀔때마다 두개씩 늘려준다.
}
System.out.println(); //줄을 바꿔준다
}
}
public static void main(String[] args) {
int star = 9;
Test2_1.Main method = new Test2_1.Main();
method.solution(star);
}
}
public class Test06 {
// 5x5 2차원 배열이 주어질 때 어떤 원소가 상하좌우에 있는 원소보다 클 때 해당 위치에 * 을
// 표시하는 프로그램을 작성하세요. 경계선에 있는 수는 상하좌우 중 존재하는 원소만을 비교합니다.
public void solution(int[][] arr1) {
int[][] answer = new int[5][5];
// 별을 담아주기 위한 배열 str1 을 선언
// (*) 은 문자열이기 때문에 정수배열의 숫자를 다시 담아준다.
String[][] str1 = new String[5][5];
// 반복문을 통해 answer 정수 배열에는 먼저 담아준다.
// 반복문을 통해 문자열 배열에 형변환을 통해 넣어준다.
for (int i = 0; i < answer.length; i++) {
for (int j = 0; j < answer.length; j++) {
answer[i][j] = arr1[i][j];
str1[i][j] = String.valueOf(answer[i][j]);
}
}
// 이중반복문을 만들어서
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
// 설명하기 참 난해한데... 이는 결국 문제상에서 요구하는 대로 하기 위해서
// 배열의 순서에서 벗어나는 케이스들에서 모조리 조건식을 걸어서 해결한다.
// 그냥 차차 읽어 보고 이해하는 게 빠를듯 하여 주석은 없다...
// 진짜 그냥 상하좌우 하나하나 상황에 맞게 빼고 더해서 조건식을 걸면 된다.
// 첫번째 줄 조건식
if (i == 0) {
if (j == 0) {
if (answer[i][j] > answer[i][j + 1]
&& answer[i][j] > answer[i + 1][j]) {
str1[i][j] = "*";
continue;
}
} else if (j > 0 && j < 4) {
if (answer[i][j] > answer[i][j - 1]
&& answer[i][j] > answer[i + j][j]
&& answer[i][j] > answer[i][j + 1]) {
str1[i][j] = "*";
continue;
}
} else if (j == 4) {
if (answer[i][j] > answer[i][j - 1]
&& answer[i][j] > answer[i + j][j]) {
str1[i][j] = "*";
}
}
}
// 2 ~ 4번째 줄 조건식
if (i > 0 && i < 4) {
if (j == 0) {
if (answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j + 1]
&& answer[i][j] > answer[i + 1][j]) {
str1[i][j] = "*";
continue;
}
} else if (j > 0 && j < 4) {
if (answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j - 1]
&& answer[i][j] > answer[i][j + 1]
&& answer[i][j] > answer[i + 1][j]) {
str1[i][j] = "*";
continue;
}
} else if (j == 4) {
if (answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j - 1]
&& answer[i][j] > answer[i + 1][j]) {
str1[i][j] = "*";
}
}
}
// 마지막 줄 조건식
if (i == 4) {
if (j == 0) {
if (answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j + 1]) {
str1[i][j] = "*";
continue;
}
} else if (j > 0 && j < 4) {
if (answer[i][j] > answer[i][j - 1]
&& answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j + 1]) {
str1[i][j] = "*";
continue;
}
} else if (j == 4) {
if (answer[i][j] > answer[i - 1][j]
&& answer[i][j] > answer[i][j - 1]) {
str1[i][j] = "*";
}
}
}
}
}
System.out.println();
for (int i = 0; i < answer.length; i++) {
for (int j = 0; j < answer.length; j++) {
System.out.print(str1[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Test06 method = new Test06();
int[][] arr1 = {{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}};
int[][] arr2 = {{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}};
method.solution(arr1);
}
}