Kursova

Diff
Login

Differences From Artifact [7f432818c7]:

To Artifact [ca8f7e069c]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


16


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>
#include <stack>
#include <array>

// Неободимо для поддержки Unicode
#ifdef _WIN32
#include <windows.h> // SetConsoleOutputCP
#else
#include <locale>
#endif

using std::cin, std::cout, std::endl, std::flush, std::setw;
using std::stack, std::tuple;



void f_rec(const size_t fun_count, const size_t max_recursion_depth, const size_t n = 1, const size_t depth = 0)


{
	cout << setw(depth) << "" << n << endl;

	if (depth >= max_recursion_depth)
		return;

	for (size_t i = 0; i < fun_count - 1; i++)
	{
		f_rec(fun_count, max_recursion_depth, (n + i) % fun_count + 1, depth + 1);
	}
}

void f_iter(const size_t fun_count, const size_t max_recursion_depth)
{
	using stack_item = tuple<size_t, size_t>;

	stack<stack_item> return_stack;
	return_stack.push({1, 0});

	while (!return_stack.empty())
	{
		auto [n, depth] = return_stack.top();
		return_stack.pop();

		stack<stack_item> queue;

		// BEGIN unmodified code
		cout << setw(depth) << "" << n << endl;

		if (depth >= max_recursion_depth)
			continue;

		for (size_t i = 0; i < fun_count - 1; i++)
		{
			queue.push({(n + i) % fun_count + 1, depth + 1});
		}
		// END unmodified code

		while (!queue.empty())
		{
			return_stack.push(queue.top());
			queue.pop();
		}
	}
}

int main()
{

	// Включаем поддержку Unicode
#ifdef _WIN32
	SetConsoleOutputCP(65001);
#else
	std::locale::global(std::locale(""));
#endif

	size_t fun_count;
	size_t max_recursion_depth;

	cout << u8"Кол-во функций: " << flush;
	cin >> fun_count;

	cout << u8"Макс. глубина рекурсии: " << flush;
	cin >> max_recursion_depth;

	cout << u8"Вывод рекурсивной функции:" << endl;
	f_rec(fun_count, max_recursion_depth);

	cout << u8"Вывод нерекурсивной функции:" << endl;
	f_iter(fun_count, max_recursion_depth);

	return 0;
}
|

|
|



|

|





>
>
|
>
>

|

|
|

|
|
|
|




|

|
|

|
|
|
|

|

|
|

|
|

|
|
|
|
|

|
|
|
|
|
|




<
|

|

|


|
|

|
|

|
|

|
|

|
|

|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <array>
#include <iomanip>
#include <iostream>
#include <stack>

// Неободимо для поддержки Unicode
#ifdef _WIN32
#  include <windows.h> // SetConsoleOutputCP
#else
#  include <locale>
#endif

using std::cin, std::cout, std::endl, std::flush, std::setw;
using std::stack, std::tuple;

void f_rec(
    const size_t fun_count,
    const size_t max_recursion_depth,
    const size_t n     = 1,
    const size_t depth = 0)
{
  cout << setw(depth) << "" << n << endl;

  if (depth >= max_recursion_depth)
    return;

  for (size_t i = 0; i < fun_count - 1; i++)
  {
    f_rec(fun_count, max_recursion_depth, (n + i) % fun_count + 1, depth + 1);
  }
}

void f_iter(const size_t fun_count, const size_t max_recursion_depth)
{
  using stack_item = tuple<size_t, size_t>;

  stack<stack_item> return_stack;
  return_stack.push({1, 0});

  while (!return_stack.empty())
  {
    auto [n, depth] = return_stack.top();
    return_stack.pop();

    stack<stack_item> queue;

    // BEGIN unmodified code
    cout << setw(depth) << "" << n << endl;

    if (depth >= max_recursion_depth)
      continue;

    for (size_t i = 0; i < fun_count - 1; i++)
    {
      queue.push({(n + i) % fun_count + 1, depth + 1});
    }
    // END unmodified code

    while (!queue.empty())
    {
      return_stack.push(queue.top());
      queue.pop();
    }
  }
}

int main()
{

  // Включаем поддержку Unicode
#ifdef _WIN32
  SetConsoleOutputCP(65001);
#else
  std::locale::global(std::locale(""));
#endif

  size_t fun_count;
  size_t max_recursion_depth;

  cout << u8"Кол-во функций: " << flush;
  cin >> fun_count;

  cout << u8"Макс. глубина рекурсии: " << flush;
  cin >> max_recursion_depth;

  cout << u8"Вывод рекурсивной функции:" << endl;
  f_rec(fun_count, max_recursion_depth);

  cout << u8"Вывод нерекурсивной функции:" << endl;
  f_iter(fun_count, max_recursion_depth);

  return 0;
}