编辑代码

#include <iostream>

using namespace std;


// 1. 斐波那契问题
int fibonacciRecursive(int n) {
    if (n <= 1) {
    return n;
    }
    return fibonacciRecursive(n-1) + fibonacciRecursive(n-2);
}

int fibonacciIterative(int n) {
    if (n <= 1) {
        return n;
    }
    int a = 0, b = 1;
    for (int i = 2; i <= n; ++i) 
    {
        int temp = b;
        b = a + b;
        a = temp;
    }
    return b;
}

// 2. 回文字符串
bool isPalindromeRecursive(const string& s) {
    string cleaned;
    for (char c : s) {
    if (isalnum(c)) {
    cleaned.push_back(tolower(c));
    }
}
if (cleaned.length() <= 1) {
    return true;
}
    return cleaned[0] == cleaned.back() && isPalindromeRecursive(cleaned.substr(1, cleaned.length()-2));
}

bool isPalindromeIterative(const string& s) {
    string cleaned;
    for (char c : s) {
    if (isalnum(c)) {
        cleaned.push_back(tolower(c));
    }
}
    int length = cleaned.length();
    for (int i = 0; i < length / 2; ++i) {
        if (cleaned[i] != cleaned[length - i - 1]) {
            return false;
        }
    }
    return true;
}

// 测试用例
int main() {


    // 斐波那契问题
    cout << "Fibonacci Recursive: " << fibonacciRecursive(7) << endl;
    cout << "Fibonacci Iterative: " << fibonacciIterative(7) << endl;
    cout << "Fibonacci Recursive: " << fibonacciRecursive(11) << endl;
    cout << "Fibonacci Iterative: " << fibonacciIterative(11) << endl;

    // 回文字符串
    cout << "Is Palindrome Recursive: " << isPalindromeRecursive("abc") << endl;
    cout << "Is Palindrome Iterative: " << isPalindromeIterative("abc") << endl;
    cout << "Is Palindrome Recursive: " << isPalindromeRecursive("abba") << endl;
    cout << "Is Palindrome Iterative: " << isPalindromeIterative("abba") << endl;

    return 0;
}