Skip to content

Commit

Permalink
fix(exercise): fib100 溢出
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 23, 2024
1 parent e0d18ad commit 7835d93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char **argv) {
std::cout << "fibonacci(20) = " << FIB20 << std::endl;

// TODO: 观察错误信息,修改一处,使代码编译运行
constexpr auto ANS_N = 100;
constexpr auto ANS_N = 90;
constexpr auto ANS = fibonacci(ANS_N);
std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl;

Expand Down
8 changes: 4 additions & 4 deletions exercises/06_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[128], cached;
static unsigned long long cache[96], cached;
// TODO: 设置正确的循环条件
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
Expand All @@ -19,8 +19,8 @@ int main(int argc, char **argv) {
ASSERT(fibonacci(3) == 2, "fibonacci(3) should be 2");
ASSERT(fibonacci(10) == 55, "fibonacci(10) should be 55");

auto fib100 = fibonacci(100);
std::cout << "fibonacci(100) = " << fib100 << std::endl;
ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371");
auto fib90 = fibonacci(90);
std::cout << "fibonacci(90) = " << fib90 << std::endl;
ASSERT(fib90 == 2880067194370816120, "fibonacci(90) should be 2880067194370816120");
return 0;
}

0 comments on commit 7835d93

Please sign in to comment.