Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,642 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,092 people online right now. Registration is fast and FREE... Join Now!




Having trouble with Josephus problem.

 
Reply to this topicStart new topic

Having trouble with Josephus problem.

c++novice
post 4 Sep, 2008 - 08:49 AM
Post #1


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 1

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.
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 4 Sep, 2008 - 10:00 AM
Post #2


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,503



Thanked 14 times

Dream Kudos: 125
My Contributions


I'd try using an array to hold the number of people.
pseudocode:
cout<<"How many people? : \n";
cin>>size;
int people[size];

cout << "Enter the number of people to skip: ";
cin >> skip;

Then just use a for loop to delete each (skip number) element in the array, until the array length =1.
Then print out your array, which will be your survivor. You can use a char or string array instead of int to
input the values (people's names or whatever).

I haven't tested this but I think it will work.

Good luck!

This post has been edited by OliveOyl3471: 4 Sep, 2008 - 10:04 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 04:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month