BackJoon Algorithm 10872 팩토리얼

업데이트:
최대 1 분 소요

BackJoon Algorithm - Java

alt

문제

alt

풀이

  • 어느 영상에서 본 댓글인데 0!은 참이므로 0! = 1 이다라는 말도 있습니다. ㅎㅎ;
  • 0! = 1로 해야 편해서 그렇게 한 거다 라는 말도 있습니다.

alt

    1!=1(기본수)×1
    2!=1(기본수)×1×2
    then..
    0!=1(기본수)

alt

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Back_10872 {
    public static void main(String[] args) throws IOException {


        // given
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());                // 정수 입력
        int result=1;
        int factorial=N;
        // when
        if(N==0){
            N=1;                                                // factorial 0은 1이다.
        }
        for(int i=0;i<N;i++){
            if(factorial==0) {
                break;
            }
            result *= factorial;
            factorial--;

        }
        // then
        System.out.println(result);
    }
}


댓글남기기