Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ class Fibonacci {

public:
// TODO: 实现构造器
// Fibonacci()
// Fibonacci
Fibonacci():cached(2){
cache[0]=0;
cache[1]=1;
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
if (i >= 16) {
throw std::invalid_argument("Index out of cache range");
}

// 计算直到需要的索引位置
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}

return cache[i];
}
};

int main(int argc, char **argv) {
// 现在类型拥有无参构造器,声明时会直接调用。
// 这个写法不再是未定义行为了。
Expand Down