having issues with my JAVA code for Josephur’s Problem: MY CODE: import java.uti
ID: 3705623 • Letter: H
Question
having issues with my JAVA code for Josephur’s Problem:
MY CODE:
import java.util.LinkedList;
public class project5 {
public static void main(String[] args) {
int n = 40; // Number of people
int k = 2; // Step over value
LinkedList<Integer> list = new LinkedList<>();
for (int i = 1; i <= n; i++){ //Initializing the LinkedList
list.addFirst(i);
}
while (list.size()!=1) {
for (int i = 0; i < k-1; i++){
list.addLast(list.removeFirst()); //Removing the first element and placing it at the end as part of step over
}
list.removeFirst(); //Killing the first element
} //Looping till 1 element exists in the list
System.out.println(list.get(list.size()-1)); // List size is now 1 and displaying the value
}
}
We are to use linked list to find the place to sit to stay alive,
for my result when 40 people are in the circle and we skip by 2 the last person should be #14 but i get #24....
Explanation / Answer
Answer:
24 is the correct answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.