Advertisement
Josif_tepe

Untitled

Feb 18th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int rec(int x) {
  8.     if(x > 5) {
  9.         return 0;
  10.     }
  11.     return x + rec(x + 1);
  12. }
  13. int main()
  14. {
  15.    
  16.     cout << rec(1) << endl;
  17.  
  18.     return 0;
  19. }
  20. // rec(1) = 1 + rec(2) = 1 + 14 = 15
  21. // rec(2) = 2 + rec(3) = 2 + 12 = 14
  22. // rec(3) = 3 + rec(4) = 3 + 9 = 12
  23. // rec(4) = 4 + rec(5) = 4 + 5 = 9
  24. // rec(5) = 5 + rec(6) = 5 + 0 = 5
  25. // rec(6) = 0
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement