Fibonacci Sum of Large Numbers(Only Last Digit to be Printed)

后端 未结 5 1953
死守一世寂寞
死守一世寂寞 2020-12-31 12:44

I have been trying to come out with a solution regarding the problem of finding the last digit of the sum of large n Fibonacci series. I have been able to pass several test

5条回答
  •  再見小時候
    2020-12-31 13:27

    For your function removing the array.

    #include "stdafx.h"
    #include 
    using namespace std;
    int calc_fib(long long int n) {
    
        int fibzero = 0;
        int fibone = 1;
        int fibnext;
        long long int res = 1;
        for (long long int i = 2; i <= n; i++) {
    
            fibnext = (fibone + fibzero) % 10;
            fibzero = fibone;
            fibone = fibnext;
            res = res + fibnext;
        }
        return (res % 10);
    }
    
    int main() 
    {
        long long int n = 0;
        std::cin >> n;
    
        std::cout << calc_fib(n) << '\n';
        return 0;
    }
    

提交回复
热议问题