fee-fi-fo-fum
[자료구조 입문] 05. 재귀 알고리즘
Algorithm🥇 2024. 1. 8. 16:24

재귀 (recursive) 팩토리얼 구하기 package 자료구조입문.재귀; public class Factorial { static int facto(int x) { //재귀로 구현 if(x > 1) { return x * facto(x-1); } return 1; } static int facto2(int x) { // 삼항연산자 + 재귀 구현 return (x > 1) ? x * facto(x-1) : 1; } public static void main(String[] args) { System.out.println(facto2(5)); } } 직접재귀와 간접재귀 직접재귀 : A - A - A 형태로 자신과 동일한 메서드를 호출하는것 간접재귀 : A - B - C 형태로 메서드 A 가 B를 호출, ..

profile on loading

Loading...