본문 바로가기
C#/백준 알고리즘

2022.01.28 [백준] C# 팩토리얼

by ian's coding 2022. 1. 28.
728x90
반응형

 

 

 

https://www.acmicpc.net/problem/10872

 

10872번: 팩토리얼

0보다 크거나 같은 정수 N이 주어진다. 이때, N!을 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

 


풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
class Program{
    static int Factorial(int a){
        if (a == 0 || a == 1)
        {
            return 1;
        }
        else
        {
            return a * Factorial(a - 1);
        }
    }
    
    static void Main()
    {
        int a = int.Parse(Console.ReadLine());
 
        Console.WriteLine(Factorial(a));
    }
}
cs

이 문제는 재귀 알고리즘을 이용하면 대표적인 문제 중 하나이다.

 

728x90
반응형

댓글