-
재귀적 팩토리얼Computer Engineering/자료구조 2019. 9. 7. 21:50
- Java
public static int factorial(int s) { if(s==1) return 1; else return (s * factorial(s-1)); } public static void main(String[] args) { System.out.println(factorial(4)); }
- Swift
func factorial(item: Int) -> Int { if item == 1 { return 1 }else { return item * factorial(item: item-1) } } print(factorial(item: 4))
반응형'Computer Engineering > 자료구조' 카테고리의 다른 글
DFS(Depth First Search) (0) 2019.09.23 BFS(Breadth First Search) (0) 2019.09.23 문자열 뒤집기 (0) 2019.09.07