백준 10872 팩토리얼 [JAVA]

Ga0·2023년 5월 31일
0

baekjoon

목록 보기
61/121

문제 해석

  • 문제는 Simple하다.
  • N을 입력받아 N!(N의 팩토리얼)을 출력받으면 된다.
팩토리얼을 구하는 식은
n! = n × (n-1) × (n-2) × (n-3) × … × 3 × 2 × 1 이런 식이다,

0!이 1! 같은 이유또한
1! = 1 x 0! = 1인데 
즉 1 X 1을 곱해서 1이 나왔기 때문에 0!이 1임을 알 수 있다.

코드

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int N = Integer.parseInt(br.readLine()); //상의 N개 하의 N개
        br.close();

        bw.write(factorial(N) + "\n");

        bw.flush();
        bw.close();
    }
    
    //팩토리얼 구하는 공식
    static int factorial(int N)
    {
        int result = 1; //0과 1 팩토리얼은 1이기 때문에 1부터 시작

        for(int i = 2; i <= N; i++)
        {
            result = result * i; //N만큼 곱한다. 숫자를 하나씩 증가시키면서
        }
        return result;
    }
}
  • 팩토리얼만 알고 있다면 크게 어려운 점이 없는 문제이다.

결과

느낀 점

  • 음... 🤨

0개의 댓글