반응형
문제
Day 9: Recursion 3 | HackerRank
Use recursion to compute the factorial of number.
www.hackerrank.com
문제 설명
재귀로 누적곱을 구하는 문제
성공 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class Solution {
// Complete the factorial function below.
static int factorial(int n) {
return (n == 1) ? 1 : n*factorial(n-1);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int result = factorial(n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
|
cs |
반응형