Solved

# Help with algorithm

Posted on 2000-02-13

I got this algorithm for a function that solves Josephus algorithm. and I will like to translate it to c++.

In C langiage:

#include <stdio.h>

#include <stdlib.h>

int josef(int num, int skip)

{

char *vec;

int i;

int curPtr;

int numLive = num;

vec = (char *)malloc(num+1);

for(i=1 ; i<=num ; i++)

vec[i] = 1;

curPtr = 1;

while(numLive > 1)

{

i = 1;

while(i<skip)

{

curPtr++;

if(curPtr > num)

curPtr = 1;

if(vec[curPtr])

i++;

}

vec[curPtr] = 0; /* KILL him*/

curPtr++;

if(curPtr > num)

curPtr = 1;

numLive--;

}

/* Find the one LIVE */

for(i=1 ; i<=num ; i++)

if(vec[i])

return(i);

return(-1);

}

What I have done so far in C++:

Jos(n,m)

for i=0 to n

{a[i]=i+1;}

while n>i do

for i=0 to n

{

if( a[i]!=0)

{

count=count+1;

if (count==m)

{

a[i]=0;

count=0;

}

}

}

I'm I doing it okay?

what else do I need?