Algorothm for finding the main element of the list

I have written algorithm, called function of Joseph. Sense such that N = 9, M = 5. Let's say there are 9 people in the circle, and after each 5 are brought together a number of removal. As a result, there will be one. Do not understand the first 2 rows int N = atoi (argv [1]);
int M = atoi (argv [2]);
that they fulfill? And where you want to assign N = 9, M = 5?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
using namespace std;
struct note
{
    int item;
    note*next;
    note(int x, note*t):item(x),next(t){}
     };
typedef note*link;
auto main(int argc, char*argv[])->int
{
    int N = atoi(argv[1]);
    int M = atoi(argv[2]);
    link t = new note(1,0);
    t->next = t;
    link x = t;
    for (int i = 2; i <= N; ++i)
        x = (x->next = new note(i, t));
    while (x != x->next)
    {
        for (int i = 1; i < M; ++i)
            x = x->next;
    }
    cout << x->item << endl;
    return 0;
}
The program is using command line arguments to pass the desired values to the program. The atoi() calls convert the second and third C-strings that are in the argument list to integers.

You would call the program using this syntax: YourProgramName 9 5

See the following link for more information.

http://www.cplusplus.com/articles/DEN36Up4/
Topic archived. No new replies allowed.