본문 바로가기
컴퓨터 공학/백준

[백준] JAVA 자바 : 팩토리얼 (10872번)

by kim-dev 2024. 1. 22.
반응형

8


팩토리얼을 구하는 문제이다.
사실 팩토리얼은 기본 중에 기본인 문제라서 깃허브에만 올릴까 하다가…
글이 많아지면 나쁠 건 없으니까 그냥 블로그에도 쓴다.

사실 팩토리얼은 순환으로 구현하는 것보다는 반복으로 구현하는 게 더 효율적이라고 배웠던 것 같은데… 순환으로 구현하는 게 더 멋지니까 나는 순환으로 구현했다~

 

import java.io.*;

public class Main{
    
    public static int stoi(String str) {
        return Integer.parseInt(str);
    }

    public static int factorial(int n) {
        if (n==0) {
            return 1;
        }
        if (n==1) {
            return n;
        } else {
            return n * factorial(n-1);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        int N = stoi(br.readLine());
        sb.append(factorial(N));
        System.out.print(sb);
        
        br.close();
    }
}

 

 

로그인

 

www.acmicpc.net

 

 

작성일자: 2023-09-10