재귀를 이용한 합계 계산
1부터 n까지의 정수 합계를 재귀 함수로 계산합니다.
#include <iostream>
using namespace std;
int sumRecur(int num) {
if (num == 1) return 1;
return num + sumRecur(num - 1);
}
int main() {
int n;
cin >> n;
cout << sumRecur(n);
return 0;
}
헤르미트 다항식 계산
주어진 x와 n에 대한 헤르미트 다항식 값을 재귀적으로 계산합니다.
#include <iostream>
#include <iomanip>
using namespace std;
double hermite(int order, double val) {
if (order == 0) return 1.0;
if (order == 1) return 2.0 * val;
return 2 * val * hermite(order - 1, val)
- 2 * (order - 1) * hermite(order - 2, val);
}
int main() {
double x;
int n;
cin >> x >> n;
cout << fixed << setprecision(2) << hermite(n, x);
return 0;
}
분수 재귀 함수 계산 1
f(x,n) = x / (n + f(x, n-1)) 점화식을 구현합니다.
#include <iostream>
#include <iomanip>
using namespace std;
double fracFunc(double x, int n) {
if (n == 1) return x / (1 + x);
return x / (n + fracFunc(x, n - 1));
}
int main() {
int n, x;
cin >> x >> n;
cout << fixed << setprecision(2) << fracFunc(x, n);
return 0;
}
분수 재귀 함수 계산 2
f(x,n) = sqrt(n + f(x, n-1)) 점화식을 구현합니다.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double sqrtFunc(double x, int n) {
if (n == 1) return sqrt(1 + x);
return sqrt(n + sqrtFunc(x, n - 1));
}
int main() {
double x;
int n;
cin >> x >> n;
cout << fixed << setprecision(2) << sqrtFunc(x, n);
return 0;
}
하노이 탑 문제
n개의 원반을 세 기둥 사이에서 이동하는 최소 경로를 출력합니다.
#include <iostream>
using namespace std;
void hanoi(int disks, char from, char via, char to) {
if (disks == 1) {
cout << from << "->" << to << endl;
return;
}
hanoi(disks - 1, from, to, via);
cout << from << "->" << to << endl;
hanoi(disks - 1, via, from, to);
}
int main() {
int n;
cin >> n;
hanoi(n, 'A', 'B', 'C');
return 0;
}
문자열 역순 출력
'!'로 종료되는 문자열을 재귀를 이용해 역순으로 출력합니다.
#include <iostream>
using namespace std;
void reverseStr() {
char ch = getchar();
if (ch == '!') {
cout << '!';
return;
}
reverseStr();
cout << ch;
}
int main() {
reverseStr();
return 0;
}
피보나치 수열 계산
재귀와 메모이제이션으로 n번째 피보나치 수를 계산합니다.
#include <iostream>
using namespace std;
long long fibMemo[100];
long long fibonacci(int n) {
if (n <= 2) return 1;
if (fibMemo[n]) return fibMemo[n];
return fibMemo[n] = fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
cin >> n;
cout << fibonacci(n);
return 0;
}
F91 함수 구현
매카시의 F91 함수를 재귀적으로 계산합니다.
#include <iostream>
using namespace std;
int f91(int num) {
if (num <= 100) return f91(f91(num + 11));
return num - 10;
}
int main() {
int input;
while (cin >> input && input != 0) {
cout << "f91(" << input << ") = " << f91(input) << endl;
}
return 0;
}
최대공약수와 최소공배수
재귀적 유클리드 알고리즘으로 GCD와 LCM을 계산합니다.
#include <iostream>
using namespace std;
long long gcdRecur(long long a, long long b) {
return b ? gcdRecur(b, a % b) : a;
}
int main() {
long long a, b;
cin >> a >> b;
long long gcd = gcdRecur(a, b);
cout << gcd << " " << (a * b) / gcd;
return 0;
}
10진수에서 8진수 변환
재귀를 사용하여 10진수를 8진수로 변환합니다.
#include <iostream>
using namespace std;
void decToOctal(int num) {
if (num / 8) decToOctal(num / 8);
cout << num % 8;
}
int main() {
int n;
cin >> n;
decToOctal(n);
return 0;
}
계단 오르기 경로 계산
1계단 또는 2계단씩 오를 때 n계단 오르는 방법의 수를 계산합니다.
#include <iostream>
using namespace std;
long long steps[100];
long long countWays(int n) {
if (n <= 2) return n;
if (steps[n]) return steps[n];
return steps[n] = countWays(n-1) + countWays(n-2);
}
int main() {
int n;
cin >> n;
cout << countWays(n);
return 0;
}