알고리즘에 대한 본격적인 시작을 하는 주차로 초급단계의 프로그래머스 문제 해결, 알고리즘 초급단계 시험
하나의 함수에서 자신을 다시 호출하여 작업을 수행하는 방식으로 주어진 문제를 푸는 방법
public class Main {
private static final StringBuilder output = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main main = new Main();
int n = sc.nextInt();
//하노이의 탑 총 횟수 구하는 공식
System.out.println((int)Math.pow(2, n) - 1);
//하노이의 탑 구현
main.hanoi(n, 1, 3, 2);
System.out.println(output);
}
void hanoi(int number, int start, int to, int via) {
if (number == 1) {
move(number, start, to);
return;
}
hanoi(number - 1, start, via, to);
move(number, start, to);
hanoi(number - 1, via, to, start);
}
void move(int number, int start , int to) {
output.append(start + " " + to + "\n");
}
}
//부모 클래스
class Parent {
String parentValue;
Parent(String parentValue) {
this.parentValue = parentValue;
}
public int setValue() {
System.out.println(this.parentValue);
}
}
//자식 클래스
class Child extends Parent {
String childValue;
Parent(String parentValue, String childValue) {
// 부모 생성자호출
super(parentValue)
this.childValue = childValue;
}
@Override
public int setValue() {
//부모 메소드 호출
super.setValue();
System.out.println(this.childValue);
}
}
interface PlayingCard {
public static final int SPADE = 4;
final int DIAMOND = 3; // public static final int DIAMOND = 3;
static int HEART = 2; // public static final int HEART = 2;
int CLOVER = 1; // public static final int CLOVER = 1;
public abstract String getCardNumber();
String getCardKing(); // public abstract String getCardKing();
}
class Unit {}
interface Fightable extends Movable, Attackable {}
interface Movable {
void move(int x, int y);
}
interface Attackable {
void attack(Unit u);
}
class InterfaceTest implements Fightable {
@Override
public void move(int x, int y) {
(구현)
}
@Override
public void attack(Unit u) {
(구현)
}
}
일단 알고리즘 문제를 계속 풀었다. 항해에서 제공해준 문제는 전체적으로 난이도가 낮은 문제들이라 전체적으로 푸는것에 어려움을 느끼지는 않았다.
쉽다 생각해서 조금더 난도 있는 문제들을 접해봤는데 알고리즘 기법들에 대한 공부를 전혀 안한 상태에서 보니까 정말 풀이방법을 생각하기 쉽지 않았다.