From 0144b21da618acf0737d3ee35b2aaa41e2064a27 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Wed, 29 Aug 2018 21:06:31 -0500 Subject: Partial Fib sum last digit done for C++ This concludes the second module of Algorithmic Design and Techniques from edX. I had a lot of fun doing it. It's challenging and I learned a lot!--- PlaygroundCpp/Sources/Playground.cpp | 57 ++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/PlaygroundCpp/Sources/Playground.cpp b/PlaygroundCpp/Sources/Playground.cpp index 4445c37..c737a3c 100644 --- a/PlaygroundCpp/Sources/Playground.cpp +++ b/PlaygroundCpp/Sources/Playground.cpp @@ -26,46 +26,39 @@ static int getLastDigit(long n) { else return result - 1; } +static int getLastDigit(long m, long n) { + if (m == n) + return getFibNModM(n); + else { + int sumFn = getLastDigit(n); + int sumFm; + if (m <= 1) + sumFm = 0; + else + sumFm = getLastDigit(m - 1); + if (sumFn >= sumFm) + return sumFn - sumFm; + else + return sumFn - sumFm + 10; + } +} -//TEST(FibSumLastDigitTest, Zero) { -// ASSERT_EQ(getLastDigit(0), 0); -//} -//TEST(FibSumLastDigitTest, One) { -// ASSERT_EQ(getLastDigit(1), 1); -//} -//TEST(FibSumLastDigitTest, Six) { -// ASSERT_EQ(getLastDigit(6), 0); -//} -//TEST(FibSumLastDigitTest, Nine) { -// ASSERT_EQ(getLastDigit(9), 8); -//} -//TEST(FibSumLastDigitTest, OneHundred) { -// ASSERT_EQ(getLastDigit(100), 5); -//} - -//TEST(FibLastDigitTest, One) { -// ASSERT_EQ(getFibNModM(239, 1000), 161); -//} -// -//TEST(FibLastDigitTest, Three) { -// ASSERT_EQ(getFibNModM(2816213588, 30524), 10249); -//} - -//TEST(FibLastDigitTest, Forty) { -// ASSERT_EQ(1344, getGCD(1344, 1344)); +//TEST(FibPartialSumLastDigit, Five_Five) { +// ASSERT_EQ(getLastDigit(5, 5), 5); //} // -//TEST(FibLastDigitTest, ThreeThreeOne) { -// ASSERT_EQ(4, getGCD(14159572, 63967072)); +//TEST(FibPartialSumLastDigit, Ten_200) { +// ASSERT_EQ(getLastDigit(10, 200), 2); //} // -//TEST(FibLastDigitTest, ReverseAB) { -// ASSERT_EQ(4, getGCD(14159572, 63967072)); +//TEST(FibPartialSumLastDigit, One_2) { +// ASSERT_EQ(getLastDigit(1, 2), 2); //} int main() { - long n; + long m, n; + std::cin >> m; std::cin >> n; - int c = getLastDigit(n); + int c = getLastDigit(m, n); std::cout << c << '\n'; } -- cgit v1.2.3