斐波那契数列
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 10; // 要计算的斐波那契数列的项数
cout << "斐波那契数列前 " << n << " 项为:" << endl;
for (int i = 0; i < n; ++i) {
cout << fibonacci(i) << " ";
}
cout << endl;
return 0;
}
约瑟夫环
#include <iostream>
using namespace std;
int josephus(int n, int k) {
if (n == 1)
return 0;
else
return (josephus(n - 1, k) + k) % n;
}
int main() {
int n = 7; // 总人数
int k = 3; // 数到第几个人出列
int winner = josephus(n, k) + 1;
cout << "胜利者的编号是: " << winner << endl;
return 0;
}