瀏覽代碼

约瑟夫问题的链表模拟方法(STL list)

zj 5 年之前
父節點
當前提交
9a8c7d0316
共有 1 個文件被更改,包括 22 次插入0 次删除
  1. 22 0
      F.cpp

+ 22 - 0
F.cpp

@@ -0,0 +1,22 @@
+#include <bits/stdc++.h>
+
+using namespace std;
+
+int main() {
+    int n, m;
+    while (cin >> n >> m) {
+        list<int> L;
+        for (int i = 1; i <= n; i++) L.push_back(i);
+        list<int>::iterator it = L.begin();
+        while (L.size() > 1) {
+            for (int k = 1; k < m; k++) {
+                ++it;
+                if (it == L.end()) it = L.begin();
+            }
+            it = L.erase(it);
+            if (it == L.end()) it = L.begin();
+        }
+        cout << *L.begin() << endl;
+    }
+    return 0;
+}