From 1a6ce2019d2b5e63944b7e345df5010312dc8691 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 2 Mar 2021 23:46:24 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=2015=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B8=20=D0=B2=20=D0=B4=D0=B2=D1=83=D1=85=D1=81?= =?UTF-8?q?=D0=B2=D1=8F=D0=B7=D0=BD=D1=8B=D1=85=20=D1=81=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/task15.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 list/task15.cpp diff --git a/list/task15.cpp b/list/task15.cpp new file mode 100644 index 0000000..46eb319 --- /dev/null +++ b/list/task15.cpp @@ -0,0 +1,58 @@ +#include +#include "list.h" + +using namespace std; + +int solve(list *&h, list *&t, int k) +{ + list *cur = h; + while (true) + { + // Пропускаем n - 1 элементов, чтобы удалить n-й. + for (int i = 0; i < k - 1; i++) + cur = cur->next; + + list *tmp = cur; + cur = cur->next; + del_node(h, t, tmp); + + // В случае, если удалённым элементом стала голова или хвост, + // список закольцуется, а иначе ничего не изменится. + t->next = h; + h->prev = t; + + // Если после текущего элемента следует он же, то в списке + // остался один элемент, который является ответом. + if (cur->next == cur) break; + } + return cur->inf; +} + +int main() +{ + int n; + cout << "n = "; + cin >> n; + + int k; + cout << "k = "; + cin >> k; + + list *head = NULL; + list *tail = NULL; + int x; + for (int i = 0; i < n; i++) + { + cin >> x; + push(head, tail, x); + } + + // Закольцовываем список + tail->next = head; + head->prev = tail; + + int result = solve(head, tail, k); + cout << result << endl; + + return 0; +} -- cgit v1.2.3