Advertisement
avr39-ripe

loopRecursion

Mar 4th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void action(int n)
  4. {
  5.     std::cout << n << '\n';
  6. }
  7. void fwLoopRecursion(int n, int max = -1)
  8. {
  9.     if (max < 0) { max = n; };
  10.     std::cout << "In" << '\n';
  11.     int i = max - n;
  12.     if (i < max - 1)
  13.     {
  14.         action(i);
  15.         fwLoopRecursion(n - 1, max);
  16.     }
  17.     else
  18.     {
  19.         action(max - 1);
  20.     }
  21.     std::cout << "Out" << '\n';
  22. }
  23.  
  24.  
  25. void fwRevLoopRecursion(int n)
  26. {
  27.     std::cout << "In" << '\n';
  28.     if (n - 1 > 0)
  29.     {
  30.         fwRevLoopRecursion(n - 1);
  31.     }
  32.     action(n - 1);
  33.     std::cout << "Out" << '\n';
  34. }
  35.  
  36. void revLoopRecursion(int n)
  37. {
  38.     std::cout << "In" << '\n';
  39.     if (n - 1 == 0)
  40.     {
  41.         action(0);
  42.     }
  43.     else
  44.     {
  45.         action(n - 1);
  46.         revLoopRecursion(n - 1);
  47.     }
  48.     std::cout << "Out" << '\n';
  49. }
  50.  
  51. int main()
  52. {
  53.     fwLoopRecursion(5);
  54.     std::cout << "##########" << '\n';
  55.     fwRevLoopRecursion(5);
  56.     std::cout << "##########" << '\n';
  57.     revLoopRecursion(5);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement