Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- void action(int n)
- {
- std::cout << n << '\n';
- }
- void fwLoopRecursion(int n, int max = -1)
- {
- if (max < 0) { max = n; };
- std::cout << "In" << '\n';
- int i = max - n;
- if (i < max - 1)
- {
- action(i);
- fwLoopRecursion(n - 1, max);
- }
- else
- {
- action(max - 1);
- }
- std::cout << "Out" << '\n';
- }
- void fwRevLoopRecursion(int n)
- {
- std::cout << "In" << '\n';
- if (n - 1 > 0)
- {
- fwRevLoopRecursion(n - 1);
- }
- action(n - 1);
- std::cout << "Out" << '\n';
- }
- void revLoopRecursion(int n)
- {
- std::cout << "In" << '\n';
- if (n - 1 == 0)
- {
- action(0);
- }
- else
- {
- action(n - 1);
- revLoopRecursion(n - 1);
- }
- std::cout << "Out" << '\n';
- }
- int main()
- {
- fwLoopRecursion(5);
- std::cout << "##########" << '\n';
- fwRevLoopRecursion(5);
- std::cout << "##########" << '\n';
- revLoopRecursion(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement