Skip to content

Instantly share code, notes, and snippets.

@htoann
Last active June 12, 2022 07:17
Show Gist options
  • Save htoann/660c0289b7507e9f971d0a387560abb6 to your computer and use it in GitHub Desktop.
Save htoann/660c0289b7507e9f971d0a387560abb6 to your computer and use it in GitHub Desktop.
Josephus
#include <bits/stdc++.h>
using namespace std;
void josephus2(int n, int k) {
queue < int > q;
for (int i = 1; i <= n; i++) q.push(i);
int d = n, v = 0, x;
while (d > 1) {
int x = q.front();
q.pop();
++v;
if (v == k) {
d--;
v = 0;
} else q.push(x);
}
cout << "\n Phan tu con sot lai la: " << q.front();
}
int main() {
josephus2(9, 2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment