You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 1.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "../checker.h"
  2. #include <vector>
  3. // READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
  4. // READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>
  5. class Fibonacci {
  6. std::vector<size_t> cache;
  7. int cached;
  8. public:
  9. // TODO: 实现动态设置容量的构造器
  10. Fibonacci(int capacity) {}
  11. // TODO: 实现复制构造器
  12. Fibonacci(Fibonacci const &other) {}
  13. // TODO: 实现正确的缓存优化斐波那契计算
  14. size_t get(int i) {
  15. }
  16. // NOTICE: 不要修改这个方法
  17. // NOTICE: 名字相同参数也相同,但 const 修饰不同的方法是一对重载方法,可以同时存在
  18. // 本质上,方法是隐藏了 this 参数的函数
  19. // const 修饰作用在 this 上,因此它们实际上参数不同
  20. size_t get(int i) const {
  21. if (i <= cached) {
  22. return cache[i];
  23. }
  24. ASSERT(false, "i out of range");
  25. }
  26. };
  27. int main(int argc, char **argv) {
  28. Fibonacci fib(12);
  29. ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
  30. Fibonacci const fib_ = fib;
  31. ASSERT(fib_.get(10) == fib.get(10), "Object cloned");
  32. return 0;
  33. }