Flavius Josephus was a famous historian of the first century, but apparently he
ID: 3853605 • Letter: F
Question
Flavius Josephus was a famous historian of the first century, but apparently he would have lived to become famous without the exercise of his computational and mathematical abilities. A legend says that during the Jewish-Roman war he was part of a band of 41 rebels trapped in a cave by the Romans. To avoid capture, the rebels decided to form a circle and proceeded to kill every third remaining person until no one was left. However, Josephus quickly calculated where he and a friend should stand to avoid all this non-sense. This time you are asked to create a program that given two positive numbers: the number of people in the circle. the elimination number, for example, if this number is exactly two we eliminate, every second person, determines the survivor's number. Example Suppose there are 10 people in a circle and the elimination number is 2 then we proceed as follows: Start with the list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and initial position at 1. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then 5 survives.Explanation / Answer
Find the program below.
package com.chegg.serial;
import java.util.LinkedList;
import java.util.Scanner;
public class CircularList {
public static void main(String[] args) {
System.out.println("Enter the no of people");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println("Enter the elimination number");
int eno = s.nextInt();
int arr[] = new int[n];
for(int i=0; i<n ;i++){
arr[i] = ++i;
}
for(int i=0; i<n ;i++){
arr = removeElements(arr, arr[eno-1]);
}
for(int i=0; i<n ;i++){
System.out.println(arr[i]);
}
}
public static int[] removeElements(int[] input, int deleteMe) {
List<Integer> result = (List) new LinkedList<Integer>();
for(int item : input)
if(deleteMe != item)
result.add(item);
return result.toArray(input);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.