C++ Map 的记忆使用

#include <iostream>
#include <map>
typedef unsigned long long ull;
using namespace std;
map<ull, ull> save;
ull F(ull n){
    if(save.count(n) != 0) return save[n];
    else if(n % 2 == 0) return F(n - 1) + 2;
    else if(n == 1) return 0;
    else return F(n - 1) + F(n - 2) - 1;
}
int main() {
    ull n;
    ull result;
    cout << "Enter n: ";
    cin >> n;
    for(ull i = 1; i <= n ; i ++){
        save.insert(pair<ull, ull>(i, F(i)));
    } 
    cout << "Result is " << F(n);
    return 0;
}

添加新评论