diff --git a/PythonCodingTest/DFS-BFS/5-4.py b/PythonCodingTest/DFS-BFS/5-4.py new file mode 100644 index 0000000..55181cd --- /dev/null +++ b/PythonCodingTest/DFS-BFS/5-4.py @@ -0,0 +1,11 @@ +# 재귀함수 - 종료조건을 항상 명시해야 한다. +# 재귀함수는 컴퓨터 내부에서 스택 자료구조를 이용하여 실행된다. + +# 100번째 종료됨 +def recursive_function(i): + if i == 100: + return + recursive_function(i+1) + print(i) + +recursive_function(20) \ No newline at end of file diff --git a/PythonCodingTest/DFS-BFS/5-5.py b/PythonCodingTest/DFS-BFS/5-5.py new file mode 100644 index 0000000..9287a72 --- /dev/null +++ b/PythonCodingTest/DFS-BFS/5-5.py @@ -0,0 +1,15 @@ +# 반복을 사용하여 구현 +def factorial_iterative(n): + result = 1 + for i in range(1, n+1): + result *= i + return result + +# 재귀를 사용하여 구현 +def factorial_recursive(n): + if n<=1: + return 1 + return n * factorial_recursive(n-1) + +print("반복", factorial_iterative(5)) +print("재귀", factorial_recursive(5)) \ No newline at end of file