๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๋ฐฑ์ค€/C++] 10872๋ฒˆ : ํŒฉํ† ๋ฆฌ์–ผ

by nitronium102 2021. 8. 24.

๋ฌธ์ œ

0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, N! ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N(0 ≤ N ≤ 12)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— N!์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

ํŒฉํ† ๋ฆฌ์–ผ์˜ ํŠน์„ฑ์„ ๊ทธ๋Œ€๋กœ ์ฝ”๋“œ๋กœ ์˜ฎ๊ฒจ๋†“์€ ๋ฌธ์ œ.

0! = 1! = 1 ์ž„์„ ์œ ์˜ํ•˜๋ฉด ๋œ๋‹ค. 

// ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ ํŒฉํ† ๋ฆฌ์–ผ ๊ตฌํ˜„
#include <iostream>
using namespace std;

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

int main(){
  int N;
  cin >> N;
  cout << factorial(N);
}

๋Œ“๊ธ€