Advertisement
DPOH-VAR

Untitled

Jul 2nd, 2020
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int getFibN(int n);
  7. int getFibNSum(int n);
  8. int TEST();
  9.  
  10. int main(){
  11.   // TESTS
  12.   int testResult = TEST(); if (testResult) return testResult;
  13.  
  14.   int n;
  15.   cout << "n=";
  16.   cin >> n;
  17.   cout << "Summ fib(0) -> fib(n) = " << getFibNSum(n);
  18. }
  19.  
  20. int getFibNSum(int n){
  21.   int sum = 0;
  22.   do {
  23.     sum += getFibN(n);
  24.   } while (n --> 1);
  25.   return sum;
  26. }
  27.  
  28. int getFibN(int n){
  29.   int a = 0;
  30.   int b = 1;
  31.   while (n --> 0){
  32.       int t = b;
  33.       b = a+b;
  34.       a = t;
  35.   }
  36.   return a;
  37. }
  38.  
  39. int TEST(){
  40.   if (getFibN(0) != 0) return 1;
  41.   if (getFibN(1) != 1) return 2;
  42.   if (getFibN(2) != 1) return 3;
  43.   if (getFibN(3) != 2) return 4;
  44.   if (getFibN(4) != 3) return 5;
  45.   if (getFibN(5) != 5) return 6;
  46.   if (getFibN(6) != 8) return 7;
  47.   if (getFibN(7) != 13) return 8;
  48.  
  49.   if (getFibNSum(0) != 0) return 9;
  50.   if (getFibNSum(1) != 1) return 10;
  51.   if (getFibNSum(2) != 2) return 11;
  52.   if (getFibNSum(3) != 4) return 12;
  53.   if (getFibNSum(4) != 7) return 13;
  54.   if (getFibNSum(5) != 12) return 14;
  55.  
  56.   return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement