Hello,
I have the Josephus problem to code in C++ and I'm stuck. Just in case, the Josephus problem is where there are a number of people in a circle and there is a number which indicates how many people that will be skipped each time around the circle. Say there are 10 people; starting with the first person in the 12 o'clock postion if the skip number is 3 then person 1 will kill person 4, person 4 must be removed everyone adjusts postions then continues with person 5 killing person 8 and so on until the survivor or surviviors remain.
I have coded a very minimal beginning but I've gotten stuck. What I don't know is how to manipulate the string in my code, to remove the killed person and slide everyone over and then continue.
Here is my code:
CODE
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
int main() {
int num;
cout << "Enter the total number of people: ";
cin >> num;
int skip;
cout << "Enter the number of people to skip: ";
cin >> skip;
cout << "\n";
string s = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
int survivor = 0;
for(int y=0; y<=num-1; y++) {
survivor ++;
cout << s[y];
}
cout << "\tk=" << skip;
cout << "\n";
cout << "\nSurvivors";
cout << "\n---------\n";
int next = 0;
int current = 0;
int start = 0;
int last = survivor-1;
while(survivor != skip) {
for(int i=0; i<=survivor-1; i++) {
cout << s[i];
}
cout << " : " << s[start] << " kills " << s[start+skip] << "\n";
s[current] = s[start+skip] + 1;
s[start] = s[current]; //Here the new start should be letter e which it is, but my output is not following suit.
//I'm assuming this is also where I would delete the letter d from the string but I just
//can't seem to figure out how.
-- survivor;
}
getch();
return 0;
}
Any help or guidance would greatly be appreciated.