on
[재귀] 백준 - 팩토리얼
[재귀] 백준 - 팩토리얼
728x90
https://www.acmicpc.net/problem/10872
-문제풀이
숫자를 입력받고 재귀함수를 통해서 팩토리얼을 계산하면 된다.
만약 숫자 n을 입력받으면 재귀함수 안에서
n이 1보다 작거나 같으면 1이 반환되도록하고
그렇지 않으면 n * (n-1) 을 재귀함수의 매개변수로 넣어준다.
-자바
package recursive; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BJ10872 { public static int recur( int a ) { if( a <= 1 ) return 1; return a * recur( a-1 ); } public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); System.out.println( recur(n) ); } }
-파이썬
from sys import stdin input = stdin.readline n = int( input() ) def recur( a ): if a <= 1: return 1 return a * recur( a-1 ) print( recur(n) )
728x90
from http://earthconquest.tistory.com/399 by ccl(S) rewrite - 2021-12-12 08:28:19