This time you are asked to create a program that given two positive numbers: the
ID: 3853540 • Letter: T
Question
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. 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.
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.