From 9eadad5d80edc766c4f8271f1f92c09b9b6595d7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 16 Nov 2023 13:37:19 +0200 Subject: [w5] Added solutions --- week05/ex4.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 week05/ex4.cpp (limited to 'week05/ex4.cpp') diff --git a/week05/ex4.cpp b/week05/ex4.cpp new file mode 100644 index 0000000..2e77c9d --- /dev/null +++ b/week05/ex4.cpp @@ -0,0 +1,33 @@ +#include + +int main() { + size_t N, nums[30]; + std::cin >> N; + for (size_t i = 0; i < N; i++) { + std::cin >> nums[i]; + } + + size_t maxJumpsIndex = 0, maxJumpsCount = 0; + bool visited[30] = { false }; + + for (size_t start = 0; start < N; start++) { + size_t current = start, jumpsCount = 0; + + while (!visited[current]) { + visited[current] = true; + current = nums[current]; + jumpsCount++; + } + + if (jumpsCount > maxJumpsCount) { + maxJumpsCount = jumpsCount; + maxJumpsIndex = start; + } + + for (size_t j = 0; j < N; j++) { + visited[j] = false; + } + } + + std::cout << maxJumpsIndex << " " << maxJumpsCount << std::endl; +} -- cgit v1.2.3