Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. // Rekurzije.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. void sagradiZid(int visina);
  10. int brojanje(int x);
  11. int Fibbonacci(int x);
  12. int potencija(int x, int y);
  13. void fibbonacci_niz(int x);
  14.  
  15. void ispisBrojeva(int i) {
  16.     cout << "Broj " << i << endl;
  17. }
  18.  
  19. void rekurzivniIspisBrojeva(int i) {
  20.     //cout << "Broj " << i << endl;
  21.     i++;
  22.     if (i < 10) {
  23.         rekurzivniIspisBrojeva(i);
  24.     }
  25. }
  26.  
  27. void beskonacnaRekurzija() {
  28.     cout << "Nemam kraja :(" << endl;
  29.     beskonacnaRekurzija();
  30. }
  31.  
  32. int faktorijela(int i) {
  33.     if (i > 1) {
  34.         return i * faktorijela(i - 1);
  35.     }
  36.     else {
  37.         return i;
  38.     }
  39. }
  40.  
  41. int zbrajanje(int x) {
  42.     if (x == 0) {
  43.         return 0;
  44.     }
  45.     return (zbrajanje(x-1) + x);
  46. }
  47.  
  48. int main()
  49. {
  50.     //fibbonacci_niz(15);
  51.  
  52.     //cout << zbrajanje(4) << endl;
  53.  
  54.    
  55.     // ispis Fib broja;
  56.     //cout << Fibbonacci(7) << endl;
  57.  
  58.     //cout << endl;
  59.    
  60.     // ispis Fib niza
  61.     /*
  62.     for (int i = 1; i < 8; i++) {
  63.         cout << Fibbonacci(i) << endl;
  64.     }
  65.     */
  66.     //cout << faktorijela(6) << endl;
  67.  
  68.     //beskonacnaRekurzija();
  69.  
  70.     /*
  71.     cout << "for petlja:" << endl;
  72.     for (int i = 1; i <= 10; i++) {
  73.         cout << "Broj " << i << endl;
  74.     }
  75.  
  76.     cout << "for petlja s funkcijom:" << endl;
  77.     for (int i = 1; i <= 10; i++) {
  78.         ispisBrojeva(i);
  79.     }
  80.    
  81.     cout << "rekurzivna funkcija:" << endl;
  82.     rekurzivniIspisBrojeva(1);
  83.     */
  84.     /*
  85.     int x, i = 0;
  86.  
  87.     cout << "Unesite duzinu Fibbonaccijevog niz: ";
  88.     cin >> x;
  89.  
  90.     while (i < x)
  91.     {
  92.         cout << endl << Fibbonacci(i);
  93.         i++;
  94.     }
  95.     */
  96.  
  97.     //cout << brojanje(5);
  98.  
  99.     // kao argument unosimo željenu visinu zida
  100.     //sagradiZid(5);
  101.  
  102.     cout << "Zbroj svih 1 i 0 je " << Fibbonacci(5) << endl;
  103.  
  104.     //cout << potencija(2, 15) << endl;
  105.  
  106.     system("PAUSE");
  107.     return 0;
  108. }
  109.  
  110. void sagradiZid(int visina) // kao parametar dajemo ukupnu visinu zida
  111. {
  112.     //cout << visina << endl;
  113.     if (visina > 0) // dok je visina (do koje trebamo izgraditi) veća od nula
  114.     {
  115.         sagradiZid(visina - 1); // pozovemo funkciju umanjenu za 1 red cigli
  116.                                 // jer je potrebna visina sad manja za 1 red
  117.     }
  118.     cout << visina << endl; // na kraju ispišemo ("sagradimo" red cigli)
  119. }
  120.  
  121. int brojanje(int x)
  122. {
  123.     if (x == 1)
  124.     {
  125.         return x;
  126.     }
  127.     else
  128.     {
  129.         return brojanje(x - 1) + x;
  130.     }
  131. }
  132.  
  133. int Fibbonacci(int x)
  134. {
  135.     if (x == 0 || x == 1)
  136.     {
  137.         cout << "Vracam " << x << endl;
  138.         return x;
  139.     }
  140.     else
  141.     {
  142.         cout << "Za " << x << " ulazim u Fibonacci(" <<
  143.             x - 1 << ") + Fibonnaci(" << x - 2 << ")" << endl;
  144.         return Fibbonacci(x - 1) + Fibbonacci(x - 2);
  145.     }
  146. }
  147.  
  148. int potencija(int x, int y)
  149. {
  150.     if (y > 1)
  151.     {
  152.         return (x * potencija(x, y - 1));
  153.     }
  154.     else
  155.         return 1;
  156. }
  157.  
  158. void fibbonacci_niz(int x) {
  159.     if (x > 0) {
  160.         fibbonacci_niz(x - 1);
  161.     }
  162.     cout << Fibbonacci(x) << endl;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement