자료구조, 알고리즘/기본다지기
[다이나믹] 팩토리얼 구하기
민쓰
2018. 8. 20. 15:49
팩토리얼
- N! = 1 x 2 x ... x N
- N! = N x (N-1)!
- f(n) = n x f(n-1)
- f(0) = 1
귀납적 계산법 코드
public int getFactorial(int n){ if(n==0){ return 1; } else return n * getFactorial(n-1); }
순차적 계산법 코드
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ans = 1; for(int i=2; i<=n; i++){ ans +=i; } System.out.println(ans);