diff options
| author | Syndamia <kamen@syndamia.com> | 2023-11-16 13:37:19 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2023-11-16 13:37:19 +0200 |
| commit | 9eadad5d80edc766c4f8271f1f92c09b9b6595d7 (patch) | |
| tree | 1f0a09839a28819d269d3f92af821629c16b0646 /week05/ex4.cpp | |
| parent | 14ae37bd8ec06b360cd756652345ee159513209e (diff) | |
| download | upp-2023-solutions-9eadad5d80edc766c4f8271f1f92c09b9b6595d7.tar upp-2023-solutions-9eadad5d80edc766c4f8271f1f92c09b9b6595d7.tar.gz upp-2023-solutions-9eadad5d80edc766c4f8271f1f92c09b9b6595d7.zip | |
[w5] Added solutions
Diffstat (limited to 'week05/ex4.cpp')
| -rw-r--r-- | week05/ex4.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
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 <iostream> + +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; +} |
