Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Josephus;
- import java.util.*;
- public class JosephusWithIterator {
- public static void main(String[] args) {
- hotPotato();
- }
- private static void hotPotato() {
- int numPeople, skip, targetIndex;
- LinkedList<Integer> list = new LinkedList<>();
- //ArrayList<Integer> list = new ArrayList<>();
- Scanner in = new Scanner(System.in);
- System.out.print("Enter the number of soldiers: ");
- numPeople = in.nextInt();
- in.nextLine();
- System.out.print("Enter the number of soldiers to skip: ");
- skip = in.nextInt();
- long startTime = System.nanoTime();
- for (int count = 1; count <= numPeople; count++) {
- list.add(count);
- }
- Iterator<Integer> it = list.iterator();
- targetIndex = skip;
- System.out.println((targetIndex + skip) % list.size() + "modulus");
- list.remove(targetIndex);
- while (it.hasNext() && list.size() > 1) {
- list.remove(targetIndex);
- if (list.size() >= 1)
- targetIndex = (targetIndex + skip) % list.size();
- }
- System.out.println(list.get(0) + " XDDDD");
- long endTime = System.nanoTime();
- MyTimer theTime = new MyTimer(startTime, endTime);
- // System.out.println("Time in seconds: " + (double) theTime.timeTaken() / 1000000000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement