Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. long long fibo_iteracyjnie (int n)
  8. {
  9.     unsigned long long x0 = 0;
  10.     unsigned long long x1 = 1;
  11.  
  12.     for (int i = 0; i < n; ++i)
  13.     {
  14.         long temp = x0 + x1;
  15.         x0 = x1;
  16.         x1 = temp;
  17.     }
  18.     return x0;
  19. }
  20.  
  21. long long fibo_rekurencyjnie(int n)
  22. {
  23.     if (n <= 1) return n;
  24.     else return fibo_rekurencyjnie(n - 2) + fibo_rekurencyjnie(n - 1);
  25. }
  26.  
  27. int main() {
  28.     int n = 50;
  29.     unsigned long long x1, x2;
  30.  
  31.     clock_t start, end;
  32.     double czas_obl;
  33.  
  34.     start = clock(); //zapamietanie aktualnego czasu systemowego
  35.     x1 = fibo_iteracyjnie(n);
  36.     end = clock();
  37.     czas_obl = 1.0 * (end - start) / CLOCKS_PER_SEC; // czas wyznaczany jest w sekundach
  38.     cout << "Iteracyjnie czas obliczen = " << czas_obl << endl;
  39.     cout << "Wynik iteracyjnie:" << x1 << endl;
  40.  
  41.     system("PAUSE");
  42.  
  43.     start = clock(); //zapamietanie aktualnego czasu systemowego
  44.     x2 = fibo_rekurencyjnie(n);
  45.     end = clock();
  46.     czas_obl = 1.0 * (end - start) / CLOCKS_PER_SEC; // czas wyznaczany jest w sekundach
  47.     cout << "Rekurencyjnie czas obliczen = " << czas_obl << endl;
  48.     cout << "Wynik rekurencyjnie:" << x2 << endl;
  49.  
  50.     system("PAUSE");
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement