fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <cassert>
  4.  
  5. using namespace std;
  6.  
  7. // return {fib_(n - 1), fib_n}
  8. pair<int, int> fib_impl(unsigned int n) {
  9. if (n == 0) return {1, 0};
  10. if (n == 1) return {0, 1};
  11. auto p = fib_impl(n - 1);
  12. return {p.second, p.first + p.second};
  13. }
  14.  
  15. int fib(unsigned int n) {
  16. return fib_impl(n).second;
  17. }
  18.  
  19. int main() {
  20. assert(fib(2) == 1);
  21. assert(fib(10) == 55);
  22. }
  23.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty