Sfoglia il codice sorgente

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

zj 5 anni fa
parent
commit
9a8c7d0316
1 ha cambiato i file con 22 aggiunte e 0 eliminazioni
  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;
+}