Question: Write a program to solve the Josephus problem, with the following modification: Sample Input: ./a.out n m p where n is the number of players
Write a program to solve the Josephus problem, with the following modification: Sample Input: ./a.out n m p where n is the number of players and m is the count used for every odd turn while p is the count used for every even turn. ./a.out 5 2 3 Sample Output: Round 1: 1 -> 3 -> 4 -> 5 Round 2: 1 -> 3 -> 4 Round 3: 1 -> 4 Round 4: 1 Winner is 1. Josephus Program to modify:
#include#include using namespace std; struct node { int key; node *next;}; int main(int argc, char* argv[]){ if(argc >2) { int i, N = atoi(argv[1]), M = atoi(argv[2]); node *t, *x; t = new node; t->key = 1; x = t; for(i = 2; i<=N; i++){ t->next = new node; t = t->next; t->key = i; } t->next = x; while(t != t->next){ for(i = 1; i next; cout << t->next->key<<" has dropped out "<< endl; x = t->next; t->next = x->next; delete x; } cout << "winner is " << t->key << endl; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
