1. 백준 17478 - 재귀함수가 뭔가요?
import java.util.Scanner;
public class Main {
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println("어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다.");
print(0);
sc.close();
}
static void print(int cur) {
if(n == cur) {
System.out.print("____".repeat(cur));
System.out.println("\"재귀함수가 뭔가요?\"");
System.out.print("____".repeat(cur));
System.out.println("\"재귀함수는 자기 자신을 호출하는 함수라네\"");
System.out.print("____".repeat(cur));
System.out.println("라고 답변하였지.");
return;
}
System.out.print("____".repeat(cur));
System.out.println("\"재귀함수가 뭔가요?\"");
System.out.print("____".repeat(cur));
System.out.println("\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.");
System.out.print("____".repeat(cur));
System.out.println("마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.");
System.out.print("____".repeat(cur));
System.out.println("그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어.\"");
print(cur+1);
System.out.print("____".repeat(cur));
System.out.println("라고 답변하였지.");
}
}
2. SWEA 1954 D2 - 달팽이 숫자
import java.util.Scanner;
class Solution {
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
int N = sc.nextInt();
// 우, 하, 좌, 상
int[] dx = { 0, 1, 0, -1 };
int[] dy = { 1, 0, -1, 0 };
int[][] arr = new int[N][N];
sb.append("#").append(i + 1).append("\n");
int x = 0, y = 0, dir = 0; // 초기 위치 및 방향
for (int j = 1; j <= N * N; j++) {
arr[x][y] = j; // 현재 위치에 숫자 삽입
int nx = x + dx[dir];
int ny = y + dy[dir];
// 범위 체크
if (nx < 0 || nx >= N || ny < 0 || ny >= N || arr[nx][ny] != 0) {
dir = (dir + 1) % 4; // 방향 전환
nx = x + dx[dir];
ny = y + dy[dir];
}
x = nx; // x 갱신
y = ny; // y 갱신
}
for(int s = 0; s < N; s++) {
for(int t = 0; t < N; t++) {
sb.append(arr[s][t]).append(" ");
}
sb.append("\n");
}
}
System.out.println(sb);
sc.close();
}
}
3. SWEA 1873 D3 - 상호의 배틀필드
- 도저히 풀 수가 없었던 문제 ㅠㅠ
- 참고: https://develop247.tistory.com/449
import java.util.Scanner;
class Solution {
static char[][] sArr;
static StringBuilder sb = new StringBuilder();
static int direction, H, W, x, y;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
H = sc.nextInt();
W = sc.nextInt();
sc.nextLine();
sArr = new char[H][W];
for (int j = 0; j < H; j++) {
String s = sc.nextLine();
for (int k = 0; k < W; k++) {
sArr[j][k] = s.charAt(k);
findStart(j, k);
}
}
sc.nextInt();
sc.nextLine();
String input = sc.nextLine();
for (char c : input.toCharArray()) {
runCommand(c);
}
sb.append("#").append(i + 1).append(" ");
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
sb.append(sArr[j][k]);
}
sb.append("\n");
}
}
System.out.println(sb);
sc.close();
}
public static void findStart(int r, int c) {
if (sArr[r][c] == '^' || sArr[r][c] == 'v' || sArr[r][c] == '<' || sArr[r][c] == '>') {
x = r;
y = c;
switch (sArr[r][c]) {
case '^':
direction = 1;
break;
case 'v':
direction = 2;
break;
case '<':
direction = 3;
break;
case '>':
direction = 4;
break;
}
}
}
public static void runCommand(char command) {
switch (command) {
case 'U':
direction = 1;
move();
break;
case 'D':
direction = 2;
move();
break;
case 'L':
direction = 3;
move();
break;
case 'R':
direction = 4;
move();
break;
case 'S':
shoot();
break;
}
}
public static void move() {
int newX = x;
int newY = y;
char status = 0;
switch (direction) {
case 1:
newX--;
status = '^';
break;
case 2:
newX++;
status = 'v';
break;
case 3:
newY--;
status = '<';
break;
case 4:
newY++;
status = '>';
break;
}
if (0 <= newX && newX < H && 0 <= newY && newY < W && sArr[newX][newY] == '.') {
sArr[x][y] = '.';
x = newX;
y = newY;
}
sArr[x][y] = status;
}
public static void shoot() {
switch (direction) {
case 1:
for (int i = x - 1; i >= 0; i--) {
if (sArr[i][y] == '*') {
sArr[i][y] = '.';
break;
}
if (sArr[i][y] == '#') {
break;
}
}
break;
case 2:
for (int i = x + 1; i < H; i++) {
if (sArr[i][y] == '*') {
sArr[i][y] = '.';
break;
}
if (sArr[i][y] == '#') {
break;
}
}
break;
case 3:
for (int i = y - 1; i >= 0; i--) {
if (sArr[x][i] == '*') {
sArr[x][i] = '.';
break;
}
if (sArr[x][i] == '#') {
break;
}
}
break;
case 4:
for (int i = y + 1; i < W; i++) {
if (sArr[x][i] == '*') {
sArr[x][i] = '.';
break;
}
if (sArr[x][i] == '#') {
break;
}
}
break;
}
}
}
728x90
반응형
'LG 유플러스 유레카 SW > 스터디' 카테고리의 다른 글
[스터디] MST 2문제 출제 및 풀기 + 도전 문제 2문제 (25.02.24) (0) | 2025.02.24 |
---|---|
[스터디] Java 알고리즘 2문제 (25.02.21) (1) | 2025.02.21 |
[스터디] Java 프로그래머스 PCCE 기출 14문제 (25.02.19) (1) | 2025.02.19 |
[스터디] Java 프로그래머스 기초 24문제 & 머쓱이 획득 (25.02.18) (1) | 2025.02.18 |
[스터디] Java 프로그래머스 기초 30문제 (25.02.17) (0) | 2025.02.17 |