Question

Write a program to solve the Josephus problem, with the following modification: Sample Input: ./a.out n...

 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.
Write in c++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#include<iostream>

#include<cstring>

#include<cstdlib>

using namespace std;

struct node{

int item;

node* next;

node(int x,node* t){

item = x;

next = t;

}

};

int main(int argc,char *argv[]){

int i;

int N = atoi(argv[1]);

int M;

int count = 1;

int m=atoi(argv[2]);

int p = atoi(argv[3]);

node* t;

t = new node(1,0);

t->next = t;

node *x;

x=t;

for(i=2;i<=N;i++){

x = (x->next = new node(i,t));

}

while(x != x->next){

node* a=x;

int k=0;

while(k <N){

cout<<a->item<<" ";

a= a->next;

k++;

}

cout<<endl;

if(count%2 == 1){

M=m;

}

else{

M=p;

}

for(i=1;i<M;i++){

x=x->next;

}

cout<<x->next->item<<"has dropped out"<<endl;

x->next = x->next->next;

count++;

N--;

}

cout<<"Winner is "<<x->item<<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a program to solve the Josephus problem, with the following modification: Sample Input: ./a.out n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • P.2. Division Write a C program to input an integer n. Then the program asks users...

    P.2. Division Write a C program to input an integer n. Then the program asks users to input n integers. Calculate the sum of all non negative integers and the division of this with every negative integer in the sequence. p3 Write a C program to print all 3-digit abc numbers, satisfying: (a+b+c)=(a.b.c). Example: 3-digit 123 number fulfills requirement as (1+2+3)=1.2.3

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • Using Dev C++, write a program to solve the problem as stated below. Write a program...

    Using Dev C++, write a program to solve the problem as stated below. Write a program will input a single integer value (all input will be exactly 8 digits long) and will store pairs of digits as individual integer values. The program will display the five 2-digit numbers as well as the average of these, each on their own line. Use a switch statement to print out Grade: and then either A (>=90), B ([80..89]), C ([70..79]), D ([60..69]) or...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • PLEASE USE INDUCTION, NO PROGRAMS ALLOWED Consider the following program, where a and n are positive...

    PLEASE USE INDUCTION, NO PROGRAMS ALLOWED Consider the following program, where a and n are positive integers. Input: a, n x = a; m = n; y = 1; while (m > 1) {if m is even x = x*x; m = m/2; if m is odd y = x*y; x = x*x; m = (m - 1)/2;} Output x*y Show by induction that the above loop has the following invariant: a^n = x^m times y. What does the above...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT