SlideShare a Scribd company logo
1 of 6
Download to read offline
The following code is based on the Josephus problem, the code does compile and gives me an
answer but it's not the right answer according to how the josephus problem works.
--------------------------------------------------------------------------------------------------------------------
---------------------------------------------
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();
}
}
/*
Output
Entre cantidad de personas
10
Entre cantidad de personas muertas
1
Muertas: 1
2 3 4 5 6 7 8 9 10 Muertas: 2
3 4 5 6 7 8 9 10 Muertas: 3
4 5 6 7 8 9 10 Muertas: 4
5 6 7 8 9 10 Muertas: 5
6 7 8 9 10 Muertas: 6
7 8 9 10 Muertas: 7
8 9 10 Muertas: 8
9 10 Muertas: 9
10El sobreviviente es: 10
Press any key to continue . . .
*/
Solution
The Josephus problem is described as:
There are n people standing in a circle waiting to be executed. The counting out begins at some
point in the circle and proceeds around the circle in a fixed direction. In each step, a certain
number of people are skipped and the next person is executed. The elimination proceeds around
the circle (which is becoming smaller and smaller as the executed people are removed), until
only the last person remains, who is given freedom. Given the total number of persons n and a
number k which indicates that k-1 persons are skipped and kth person is killed in circle.
The task is to choose the place in the initial circle so that you are the last one remaining and so
survive.
The logic should be like this:
josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1
josephus(1, k) = 1
Program to test the above logic
int main()
{
int n = 17;
int k = 5;
printf("The chosen place is %d", josephus(n, k));
return 0;
}

More Related Content

Similar to The following code is based on the Josephus problem, the code does c.pdf

Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
Create a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfCreate a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfarihantsherwani
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listingChris Worledge
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Mozaic Works
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already KnowKevlin Henney
 
Compilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMCompilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMLinaro
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Ziyauddin Shaik
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performanceintelliyole
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfJkPoppy
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 
Oct13' ----
Oct13' ----Oct13' ----
Oct13' ----Tak Lee
 

Similar to The following code is based on the Josephus problem, the code does c.pdf (20)

Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
Create a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfCreate a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdf
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listing
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
Python Programming
Python Programming Python Programming
Python Programming
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Compilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMCompilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVM
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Oct13' ----
Oct13' ----Oct13' ----
Oct13' ----
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 

More from ezhilvizhiyan

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfezhilvizhiyan
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfezhilvizhiyan
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfezhilvizhiyan
 
Discuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdfDiscuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdfezhilvizhiyan
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfezhilvizhiyan
 
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdfContinuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdfezhilvizhiyan
 
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdfAdmitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdfezhilvizhiyan
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdfezhilvizhiyan
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdfezhilvizhiyan
 
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdfezhilvizhiyan
 
X Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdfX Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdfezhilvizhiyan
 
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdfWhy did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdfezhilvizhiyan
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfezhilvizhiyan
 
Which of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdfWhich of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdfezhilvizhiyan
 
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdfTure or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdfezhilvizhiyan
 
This is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdfThis is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdfezhilvizhiyan
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfezhilvizhiyan
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfezhilvizhiyan
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfezhilvizhiyan
 
QUESTION 9 Research on North American women serving as expatriates fo.pdf
QUESTION 9 Research on North American women serving as expatriates fo.pdfQUESTION 9 Research on North American women serving as expatriates fo.pdf
QUESTION 9 Research on North American women serving as expatriates fo.pdfezhilvizhiyan
 

More from ezhilvizhiyan (20)

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdf
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdf
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdf
 
Discuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdfDiscuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdf
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
 
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdfContinuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
 
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdfAdmitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
 
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
 
X Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdfX Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdf
 
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdfWhy did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
 
Which of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdfWhich of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdf
 
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdfTure or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
 
This is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdfThis is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdf
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdf
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdf
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
 
QUESTION 9 Research on North American women serving as expatriates fo.pdf
QUESTION 9 Research on North American women serving as expatriates fo.pdfQUESTION 9 Research on North American women serving as expatriates fo.pdf
QUESTION 9 Research on North American women serving as expatriates fo.pdf
 

Recently uploaded

Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 

Recently uploaded (20)

Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

The following code is based on the Josephus problem, the code does c.pdf

  • 1. The following code is based on the Josephus problem, the code does compile and gives me an answer but it's not the right answer according to how the josephus problem works. -------------------------------------------------------------------------------------------------------------------- --------------------------------------------- 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() {
  • 2. 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;
  • 3. 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();
  • 4. 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;
  • 5. 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(); } } /* Output Entre cantidad de personas 10 Entre cantidad de personas muertas 1 Muertas: 1 2 3 4 5 6 7 8 9 10 Muertas: 2 3 4 5 6 7 8 9 10 Muertas: 3 4 5 6 7 8 9 10 Muertas: 4 5 6 7 8 9 10 Muertas: 5 6 7 8 9 10 Muertas: 6 7 8 9 10 Muertas: 7 8 9 10 Muertas: 8 9 10 Muertas: 9
  • 6. 10El sobreviviente es: 10 Press any key to continue . . . */ Solution The Josephus problem is described as: There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. The logic should be like this: josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1 josephus(1, k) = 1 Program to test the above logic int main() { int n = 17; int k = 5; printf("The chosen place is %d", josephus(n, k)); return 0; }