josephus I done but II is not that easy
This commit is contained in:
23
CSES - CSES Problem Set/Josephus_Problem_II.cpp
Normal file
23
CSES - CSES Problem Set/Josephus_Problem_II.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// Josephus Problem II
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
//There are Solution with sqrt decomposition and segment trees
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ios::sync_with_stdio(0);
|
||||||
|
cin.tie(0);
|
||||||
|
cout.tie(0);
|
||||||
|
int n, k; cin >> n >> k;
|
||||||
|
set<int> children;
|
||||||
|
for (int i = 1; i <= n; i++) children.insert(i);
|
||||||
|
|
||||||
|
int i = (k % children.size());
|
||||||
|
while (!children.empty()) {
|
||||||
|
cout << *children.begin() + i << " ";
|
||||||
|
children.erase(children.begin() + i);
|
||||||
|
// cout << "S: "<< children.size() << endl;
|
||||||
|
if (!children.empty()) i = (i + k) % children.size();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
// Josephus Problem I
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ios::sync_with_stdio(0);
|
||||||
|
cin.tie(0);
|
||||||
|
cout.tie(0);
|
||||||
|
int n; cin >> n;
|
||||||
|
queue<int> children;
|
||||||
|
for (int i = 1; i <= n; i++) children.push(i);
|
||||||
|
bool flag = true;
|
||||||
|
while (!children.empty()) {
|
||||||
|
int ele = children.front();
|
||||||
|
children.pop();
|
||||||
|
if (flag) {
|
||||||
|
children.push(ele);
|
||||||
|
} else {
|
||||||
|
cout << ele << " ";
|
||||||
|
}
|
||||||
|
flag = !flag;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user