-
์๊ธฐ ์์ ์ ๋ค์ ํธ์ถํ๋ ํจ์ : recursive_function()
-
ํน์ ํ ํจ์๊ฐ ์๊ธฐ์์ ์ ํฌํจ
-
์ฌ๊ทํจ์๋ ๋ด๋ถ์ ์ผ๋ก ์คํ ์๋ฃ๊ตฌ์กฐ์ ๋์ผ
โ ์ด์ : ํจ์๋ฅผ ๊ณ์ ํธ์ถ์ ๊ฐ์ฅ ๋ง์ง๋ง์ ํธ์ถํ๊ธฐ ํจ์๊ฐ ๋จผ์ ์ํ์ ๋๋ด์ผ ๊ทธ ์์ ํจ์ ํธ์ถ์ด ์ข ๋ฃ๋๊ธฐ ๋๋ฌธ
-
์คํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํด์ผ ํ๋ ์๋น์๋ ์ฌ๊ท ํจ์๋ฅผ ์ด์ฉํด์ ๊ฐํธํ๊ฒ ๊ตฌํ ๊ฐ๋ฅ
- ํ์ด์ฌ ์์
- ๋ฌธ์์ด์ด ๋ฌดํํ ์ถ๋ ฅํ๋ค๊ฐ ์ฌ๊ท์ ์ต๋ ๊น์ด ์ด๊ณผ์ ์ค๋ฅ๊ฐ ๋ธ
def recursive_function(): # def๋ ํจ์๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ ์์ฝ์ด
print('์ฌ๊ท ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.')
recursive_function()
recursive_function() - 1์ ์์ฐ์
- ์์ฐ์ n์ ๋ฐ๋ก ๋ค์ ์ ์๋ ์์ฐ์์ด๋ค.
- ์ด์ง๊ฒ์ํธ๋ฆฌ ํ์ฉ๋๋ ์๊ณ ๋ฆฌ์ฆ ์ด๋ค.
- ํฉํ ๋ฆฌ์ผ ๊ตฌํ๊ธฐ
- ์์ด ์๋ ์ ์์ ํฉํ ๋ฆฌ์ผ๊ฐ
0! = 1
n>0 ์ด๋ฉด n! = n x (n-1)!
5!
5x4x3x2x1= 120
int result = 0
for(int i = 5; i<0;i--){
result = result * i;
}- ์์ ๊ณผ ๋๊ฐ์ ๊ตฌ์กฐ์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ฐฉ์
- ๋ฌธ์ ) 0 ์ด์์ ๋์ ์ n, m ์ด ์ฃผ์ด์ก์๋ n ๊ตฌํ์์ค
- ์ํ ์ ์
- ์ฌ๊ท๋ ๋ถ๋ถ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ํ์ด ๋ฐฉ๋ฒ
- ๋ถ๋ถ๋ฌธ์ ๋ ๊ฐ๊ฐ ํ๋์ ๋ช ํํ ๋ฌธ์ ๋ฅผ ๋ํ๋ด์ด ํ๋์ ๋ต์ ๋ผ ์ ์์ด์ผ ํ๋ค.
- ๋ต์ ๋ด๋๋ฐ๋ ์ ๋ ฅ๋๋ ๋ณ์
- ์ด๋ ๊ฒ ๋ต์ ๊ฒฐ์ ํ๋ ๋ณ์๋ค์
์ํ(state) - ํ๋์ ์ํ์ ๋ํ ๋ต์ ์ฐพ๋ ๋ฌธ์
- 2๊ฐ์ ๋ณ์๊ฐ ์์ผ๋ฏ๋ก, ์ํ(n,m) ์ ์ํ์ฌ ํ๊ธฐ
- ์ข ๋ฃ ์กฐ๊ฑด
- ๋ถ๋ถ ๋ฌธ์ ๋ค์ ์์ฑํ์ฌ ํด๊ฒฐํด ๋๊ฐ๋ค๊ฐ ์ธ์ ๊ฐ๋ ์ข ๋ฃํด์ผ ํ๋ค.
- ๋ต์ด ๋์ค๋ ์ํ๋ฅผ ๊ฒ์ฌํ์ฌ ๋ฐํํ ์ ์๋๋ก ์ข ๋ฃ์กฐ๊ฑด
- n์ 0์ ๊ณฑ์ ๊ตฌํ ๋ (m=0) ๋ต 1
- 0์ m์ ๊ณฑ ๊ตฌํ ๋(n=0) 1
- 1์ m์ ๊ณฑ (n= 1) 1
(n,0) = 1
(0,m) =1
(1,m) =1
(n,m) = n*(n,m-1)import java.util.Scanner;
public class powerEx {
private static int power(int n, int m){
if (m == 0 ) return 1;
if (n == 0) return 1;
if (n==0) return 1;
return n*power(n, m-1);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int result = power(n, m);
System.out.printf("%d์ %d ๊ฑฐ๋ญ์ ๊ณฑ ๊ฐ : %d", n, m, result);
}
}import java.util.Scanner;
public class Gcd {
private static int gCD(int x, int y){
if(y == 0) return x;
else return gCD(y, x%y);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int result = gCD(n, m);
System.out.printf("%d์ %d ์ต๋๊ณต์ฝ์ : %d", n, m, result);
}
}์ธ ๊ฐ์ ๊ธฐ๋ฅ๊ณผ ์ํ๋ค์ด ์ฃผ์ด์ก์ ๋, ํ ๊ธฐ๋ฅ์์ ๋ค๋ฅธ ๊ธฐ๋ฅ์ผ๋ก ๋ชจ๋ ์ํ์ ์ฎ๊ธฐ๋ ๋ฌธ์ ์ด๋ฉฐ, ์ํ๋ค์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ค๋ฅด๋ฉฐ, ํฐ ์ํ ์์ ์์ ์ํ์ด ์ฌ๋ผ๊ฐ์ผ ํ๋ค.
ํต์ฌ : ๋ชฉํ๋ ๋ชจ๋ ์ํ์ ๊ธฐ๋ฅ A์์ ๊ธฐ๋ฅ C๋ก ์ฎ๊ธฐ๋ ๊ฒ
- ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/12946
- ๊ท์น
- ํ ๋ฒ์ ํ ๊ฐ์ ์ํ๋ง ์ด๋ ๊ฐ๋ฅ
- ํฐ ์ํ ์์ ์์ ์ํ ์ฌ๋ฆด ์ ์์
- ์ด๋ค ๊ธฐ๋ฅ ์์ ์๋ ์ํ์ ๊ทธ ๊ธฐ๋ฅ์ผ๋ก๋ง ์ด๋์ํฌ ์ ์์
- ๋ถ์
๊ฒฐ๋ก
- ์ํ์ ๊ฐ์๊ฐ 1๊ฐ์ผ ๊ฒฝ์ฐ : ๋ฐ๋ก ๊ธฐ๋ฅ A์์ ๊ธฐ๋ฅ C๋ก ์ฎ๊ธธ ์ ์๋ค.
- ์ํ์ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ธ ๊ฒฝ์ฐ :
- ๊ฐ์ฅ ์์ ์ํ์ ์ ์ธํ ๋๋จธ์ง ์ํ๋ค์ ๊ธฐ๋ฅ A์์ ๊ธฐ๋ฅ B๋ก ์ฎ๊ธด๋ค.
- ๊ฐ์ฅ ํฐ ์ํ์ ๊ธฐ๋ฅ A์์ ๊ธฐ๋ฅ C๋ก ์ฎ๊ธด๋ค.
- ๊ธฐ๋ฅ B์ ์๋ ์ํ๋ค์ ๊ธฐ๋ฅ C๋ก ์ฎ๊ธด๋ค.
- ์ํ(๋ณ์)
- ์ฎ๊ธฐ๋ ค๋ ์ํ์ ๊ฐ์ n
- ์ํ์ด ํ์ฌ ์์นํ๋ ๊ธฐ๋ฅ๊ฐ from
- ์ํ์ด ์ด๋ํด์ผ ํ๋ ๊ธฐ๋ฅ to(n, from, to)
- ex. ์ํ n๊ฐ๋ฅผ ๊ธฐ๋ฅ 1์์ ๊ธฐ๋ฅ 3์ผ๋ก ์ฎ๊ฒจ๋ผ! (n, 1, 3)
- (n, from, to) โ ์ํ n๊ฐ๋ฅผ ๊ธฐ๋ฅ from์์ ๊ธฐ๋ฅ to ์ฎ๊ธฐ๋ ๊ณผ์
- ์ข ๋ฃ์กฐ๊ฑด
- ์ํ์ด 1์ผ ๋ ์ข ๋ฃ
- (1, form, to) = [from, to] โ ์ํ์ด 1๊ฐ์ผ ๋๋ from์์ to๋ก ์ฎ๊ฒจ๋ผ
- ์ ํ์
- ์ํ : (n, from, to)
- ์ข ๋ฃ : (1, from, to)
- ์ ์ด์ํ : (n-1, from, to)
4 . ์ฝ๋
import java.util.ArrayList;
import java.util.List;
class Solution {
private List<int[]> hanoi(int n, int from, int to){
if(n == 1) return List.of(new int[] {from, to}); //์ข
๋ฃ์กฐ๊ฑด n==1 ์ผ๋ from to ์ด๋๋ง ์์ผ๋ผ
//์ ํ์ ๊ตฌํ
int empty = 6 - from - to;
List<int[]> result = new ArrayList<>();
result.addAll(hanoi(n-1,from,empty));
result.addAll(hanoi(1,from,to));
result.addAll(hanoi(n-1,empty,to));
return result;
}
public int[][] solution(int n) {
return hanoi(n,1,3).toArray(new int[0][]);
}
}- ๋ค๋ฅธ ์ฌ๋ ์ฝ๋
import java.util.Arrays;
class Hanoi {
public int[][] hanoi(int n) {
// 2์ฐจ์ ๋ฐฐ์ด์ ์์ฑํด ์ฃผ์ธ์.
int[][] answer = {{1,3}};
System.out.println(n);
return answer;
}
// ์๋๋ ํ
์คํธ๋ก ์ถ๋ ฅํด ๋ณด๊ธฐ ์ํ ์ฝ๋์
๋๋ค.
public static void main(String[] args) {
Hanoi h = new Hanoi();
System.out.println(Arrays.toString(h.hanoi(2)));
}
}- ํ๋ ธ์ด ํ์ ๋์์๋ฆฌ๋ฅผ ์ ์ ์์
// ํ๋
ธ์ด์ ํ
package recurive;
import java.util.Scanner;
class Hanoi {
//--- no๊ฐ์ ์๋ฐ์ x๋ฒ ๊ธฐ๋ฅ์์ y๋ฒ ๊ธฐ๋ฅ์ผ๋ก ์ฎ๊น ---//
static void move(int no, int x, int y) {
if (no > 1)
move(no - 1, x, 6 - x - y);
System.out.printf("์๋ฐ[%d]๋ฅผ %d๋ฒ ๊ธฐ๋ฅ์์ %d๋ฒ ๊ธฐ๋ฅ์ผ๋ก ์ฎ๊น\n", no, x, y);
if (no > 1)
move(no - 1, 6 - x - y, y);
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("ํ๋
ธ์ด์ ํ");
System.out.print("์๋ฐ์ ๊ฐ์ : ");
int n = stdIn.nextInt();
move(n, 1, 3); // ์ 1 ๊ธฐ๋ฅ์ ์์ธ n๊ฐ๋ฅผ ์ 3 ๊ธฐ๋ฅ์ผ๋ก ์ฎ๊น
}
}ํด์ค : https://ksb-dev.tistory.com/273
- A, E, I, O, U ๋ง์ ์ฌ์ฉํ์ฌ 5์๋ฆฌ ์ด๋ด์ ๋ฌธ์๋ฅผ ๋ง๋ค๊ณ , ์ ๋ ฌํ์ฌ ์ฃผ์ด์ง๋ ๋ฌธ์๊ฐ ๋ช ๋ฒ์งธ์ธ์ง ๋ฐํํด์ผ ํจ
- ์ฒซ ๋ฒ์งธ ๋จ์ด๋ "A"์ด๊ณ , ๋ ๋ฒ์งธ ๋จ์ด๋ "AA"์ด๋ฉฐ, ๋ง์ง๋ง ๋จ์ด๋ "UUUUU"
- ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/84512
- ๋ถ์
- ์ํ(word) : (word)๋ก ์์๋๋ ๋ชจ๋ ๋จ์ด
- ์ข ๋ฃ์กฐ๊ฑด : (๊ธธ์ด๊ฐ 5์ธ word) = word
- ์ ํ์(word) =[word]+[word+'A']+[word+'E']+[word+'I']+[word+'O']+[word+'U']
- ์ข ๋ฃ์กฐ๊ฑด 5์ผ ๋, ๋ฐ๋ก word ๋ฐํ
- ๋ถ๋ถ๋ฌธ์ ๋ฅผ ํด๊ฒฐ
private List<String> generate(String word){
//์ข
๋ฃ ์กฐ๊ฑด, ์ ํ์ ๊ตฌํ
}
// ์ข
๋ฃ์กฐ๊ฑด 5์ผ ๋, ๋ฐ๋ก word ๋ฐํ
private List<String> generate(String word){
//์ข
๋ฃ ์กฐ๊ฑด , ์ ํ์ ๊ตฌํ
List<String> wrods = new ArrayList<>();
words.add(word);
if(word.length() == 5) return words;
//์ ํ์
}private List<String> generate(String word){
//์ข
๋ฃ ์กฐ๊ฑด , ์ ํ์ ๊ตฌํ
List<String> wrods = new ArrayList<>();
words.add(word);
if(word.length() == 5) return words;
//์ ํ์ :์ฌ์ฉ๋ ์ ์๋ ๋ชจ๋ ๋ฌธ์๋ฅผ words ๋ถ์ฌ์ผ ํ๋ค.
private static fianl char[] CHARS = "AEIOU".toCharArray();
}- ์ฝ๋
import java.util.ArrayList;
import java.util.List;
class Solution {
private static final char[] CHARS = "AEIOU".toCharArray();
private List<String> generate(String word){
List<String> words = new ArrayList<>();
words.add(word);
if(word.length() == 5) return words;
for(char c : CHARS){
words.addAll(generate(word + c));
}
return words;
}
public int solution(String word) {
return generate("").indexOf(word);
}
}- ๋ค๋ฅธ ์ฌ๋ ํ์ด
import java.util.*;
class Solution {
List<String> list = new ArrayList<>();
void dfs(String str, int len) {
if(len > 5) return;
list.add(str);
for(int i = 0; i < 5; i++) dfs(str + "AEIOU".charAt(i), len + 1);
}
public int solution(String word) {
dfs("", 0);
return list.indexOf(word);
}
}class Solution {
public int solution(String word) {
int answer = 0, per = 3905;
for(String s : word.split("")) answer += "AEIOU".indexOf(s) * (per /= 5) + 1;
return answer;
}
}



