From 59242266b3c404835e817835a08af7e61537400a Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 8 Feb 2023 12:20:07 +0800 Subject: [PATCH] s0509 --- include/s0509_fibonacci_number.hpp | 13 +++++++++++++ src/s0509_fibonacci_number.cpp | 9 +++++++++ tests/s0509_fibonacci_number.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 include/s0509_fibonacci_number.hpp create mode 100644 src/s0509_fibonacci_number.cpp create mode 100644 tests/s0509_fibonacci_number.cpp diff --git a/include/s0509_fibonacci_number.hpp b/include/s0509_fibonacci_number.hpp new file mode 100644 index 0000000..8d60efb --- /dev/null +++ b/include/s0509_fibonacci_number.hpp @@ -0,0 +1,13 @@ +#ifndef S0509_FIBONACCI_NUMBER_HPP +#define S0509_FIBONACCI_NUMBER_HPP + +#include + +using namespace std; + +class S0509 { + public: + int fib(int n); +}; + +#endif diff --git a/src/s0509_fibonacci_number.cpp b/src/s0509_fibonacci_number.cpp new file mode 100644 index 0000000..872b50e --- /dev/null +++ b/src/s0509_fibonacci_number.cpp @@ -0,0 +1,9 @@ +#include "s0509_fibonacci_number.hpp" + +int S0509::fib(int n) { + vector dp{0, 1}; + for (int i{2}; i <= n; ++i) { + dp.push_back(dp[i - 1] + dp[i - 2]); + } + return dp[n]; +} diff --git a/tests/s0509_fibonacci_number.cpp b/tests/s0509_fibonacci_number.cpp new file mode 100644 index 0000000..477cee4 --- /dev/null +++ b/tests/s0509_fibonacci_number.cpp @@ -0,0 +1,24 @@ +#include "s0509_fibonacci_number.hpp" + +#include + +TEST(Problem509, Case1) { + int n{2}; + int expected{1}; + S0509 solution; + EXPECT_EQ(solution.fib(n), expected); +} + +TEST(Problem509, Case2) { + int n{3}; + int expected{2}; + S0509 solution; + EXPECT_EQ(solution.fib(n), expected); +} + +TEST(Problem509, Case3) { + int n{4}; + int expected{3}; + S0509 solution; + EXPECT_EQ(solution.fib(n), expected); +}