Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1.  
  2.  
  3. package Josephus;
  4.  
  5. import java.util.*;
  6.  
  7.  
  8. public class JosephusWithIterator {
  9. public static void main(String[] args) {
  10.  
  11.  
  12. hotPotato();
  13.  
  14.  
  15. }
  16.  
  17.  
  18. private static void hotPotato() {
  19.  
  20. int numPeople, skip, targetIndex;
  21. LinkedList<Integer> list = new LinkedList<>();
  22. //ArrayList<Integer> list = new ArrayList<>();
  23.  
  24. Scanner in = new Scanner(System.in);
  25.  
  26.  
  27. System.out.print("Enter the number of soldiers: ");
  28. numPeople = in.nextInt();
  29. in.nextLine();
  30.  
  31.  
  32. System.out.print("Enter the number of soldiers to skip: ");
  33. skip = in.nextInt();
  34.  
  35. long startTime = System.nanoTime();
  36. for (int count = 1; count <= numPeople; count++) {
  37. list.add(count);
  38. }
  39. Iterator<Integer> it = list.iterator();
  40. targetIndex = skip;
  41.  
  42. System.out.println((targetIndex + skip) % list.size() + "modulus");
  43.  
  44. list.remove(targetIndex);
  45. while (it.hasNext() && list.size() > 1) {
  46. list.remove(targetIndex);
  47.  
  48. if (list.size() >= 1)
  49. targetIndex = (targetIndex + skip) % list.size();
  50.  
  51.  
  52. }
  53.  
  54. System.out.println(list.get(0) + " XDDDD");
  55.  
  56.  
  57. long endTime = System.nanoTime();
  58. MyTimer theTime = new MyTimer(startTime, endTime);
  59. // System.out.println("Time in seconds: " + (double) theTime.timeTaken() / 1000000000);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement