[Java | 백준 10872] 팩토리얼
· 테크· Java
Java
백준 10872 www.acmicpc.net/problem/10872
풀이
팩토리얼 개념을 알아야 문제를 풀 수 있다. 팩토리얼은 n! 이렇게 표현되며, 1 부터 n까지의 수를 곱한 결과값을 갖는다.
즉 3! = 1 * 2 * 3 이다.
재귀 메서드를 만들어서 다음과 같이 팩토리얼 값을 구할 수 있다. 단, 1 보다 작거나 같은 값은 1이 나오도록 제외해준다. (예제 입력 2의 0의 팩토리얼은 1이다.)
|
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
|
import java.util.Scanner;
public class math_10872 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
sc.nextLine();
System.out.println(fac(a));
sc.close();
}
private static int fac(int a) {
if(a<=1) {
return 1;
}
else {
return a*fac(a-1);
}
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319