Question

In: Statistics and Probability

language C++ i need output, Pleases The Josephus problem is named after the historian Flavius Josephus,...

language C++

i need output, Pleases

The Josephus problem is named after the historian Flavius Josephus, who lived between

the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against

the Roman Empire. When it appeared that Josephus and his band were to be captured,

they resolved to kill themselves. Josephus persuaded the group by saying, “Let us commit

our mutual deaths to determination by lot. He to whom the first lot falls, let him be

killed by him that hath the second lot, and thus fortune shall make its progress through

us all; nor shall any of us perish by his own right hand, for it would be unfair if, when

the rest are gone, somebody should repent and save himself” (Flavius Josephus, The

Wars of the Jews, Book III, Chapter 8, Verse 7, tr. William Whiston, 1737). Yet that is

exactly what happened; Josephus was left for last, and he and the person he was to kill

surrendered to the Romans. Although Josephus does not describe how the lots were

assigned, the following approach is generally believed to be the way it was done. People

form a circle and count around the circle some predetermined number. When this number

is reached, that person receives a lot and leaves the circle. The count starts over with

the next person. Using the circular linked list developed in Exercise 6, simulate this problem.

Your program should take two parameters: n, the number of people that start, and

m, the number of counts. For example, try n = 20 and m = 12. Where does Josephus need

to be in the original list so that he is the last one chosen?

Solutions

Expert Solution


class Node
{

public int iData;
public Node next;

public Node(int x) {
iData = x;
}

public void displayNode() {
System.out.print(iData + " ");

}

}
class CircularList
{

private Node first;
private Node last;
private Node current;
private int count; // total items in the list
public CircularList getCurrent;

public CircularList() {
first = null;
last = null;
current = null;
count = 0;
}

public boolean isEmpty() {
return first == null;
}

public void step() {
current = current.next;
}

public Node getCurrent() {
return current;
}

public Node getFirst() {
return first;
}

public void insert(int x) {
Node newNode = new Node(x);

if (isEmpty()) {
first = newNode;
current = first;
} else {
current.next = newNode;
}

newNode.next = first;
last = newNode;
step();
count++;
}

public boolean search(int x) {
Node search = first;
int y = 0;

while (search.iData != x && y < count) {
search = search.next;
y++;
}

if (search.iData == x) {
System.out.println("Found the value: " + search.iData);
return true;
} else {
System.out.println("Value not found in list");
return false;
}

}

public void delete(int x) {
Node prev = first;
Node curr = first.next;

while (curr.iData != x) {
prev = curr;
curr = curr.next;

}

if (count == 1) {
first = null;
count--;
} else if (curr == first) {
prev.next = curr.next;
first = curr.next;
count--;
} else {
prev.next = curr.next;
count--;
}

}

public void displayList() {
int x = 0;
Node printer = first;

while (x < count) {
printer.displayNode();
printer = printer.next;
x++;
}
System.out.println("");

}

}
class Josephus
{

private int numOfPeople; // number of people in a circle
private int countNum; // number used for counting off
private Node head;
private Node tail;
CircularList circle;

public Josephus() {
circle = new CircularList();
numOfPeople = 0;
countNum = 0;

}

public void setNumOfPeople(int x) {
numOfPeople = x;

}

public int getNumOfPeople() {
return numOfPeople;
}

public void setCountNum(int x) {
countNum = x;
}

public int getCountNum() {
return countNum;
}

public void addPeople() {
for (int i = 1; i < numOfPeople; i++) {
circle.insert(i);
}
}

public void move() {

for (int i = 0; i < countNum; i++) {
tail = head;
head = head.next;
}

System.out.println("KILLED : " + head.iData);

}

public void execute() {
tail = null;
head = circle.getFirst();
  
while (numOfPeople != 2) {
  
move();
circle.delete(head.iData);
tail = tail.next;
head = head.next;
numOfPeople--;
display();
}

}

public void display() {
System.out.print("Alive: ");
circle.displayList();
}

}
public class Test
{
   public static void main(String[] args) {
   Josephus suicide = new Josephus();

   suicide.setNumOfPeople(20);
   suicide.addPeople();
   suicide.display();

   suicide.setCountNum(12);
   suicide.execute();

   }

}

NOTE:: I HOPE YOUR HAPPY WITH MY ANSWER.....***PLEASE SUPPORT ME WITH YOUR RATING.....THANK YOU....


Related Solutions

C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
C++: (I need this program to output in a certain format I will post the "needed...
C++: (I need this program to output in a certain format I will post the "needed results" below) To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letter. Write a program...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors...
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors who destroys and against Lizard who bursts,ties with himself, and loses to Rock Covering Paper and Spock that vaporizes the stone. - Role: Win against Stone who he covers and against Spock who refutes, tie with himself, and loses against Scissors who cut it and against Lizard who eat. - Scissors: Win against Paper who cuts and against Lizard who decapitates, tie with himself,...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage The user can run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT