Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aud6zad8;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
- public class LuckySuitor {
- public static final int RIGHT = 1; // --->
- public static final int LEFT = -1; // <---
- public static int whoIsLucky(int n) {
- List<Integer> positions = new ArrayList<Integer>();
- for (int i = 1; i <= n; i++) {
- positions.add(i);
- }
- ListIterator<Integer> it = positions.listIterator();
- printList(positions);
- while (true) {
- int dir = RIGHT;
- int count = 0;
- while (it.hasNext() && dir == RIGHT) {
- it.next();
- count++;
- if (count == 3) {
- count = 0;
- it.remove();
- printList(positions);
- }
- if (!it.hasNext()) {
- it.previous();
- dir = LEFT;
- break;
- }
- }
- while (it.hasPrevious() && dir == LEFT) {
- count++;
- it.previous();
- if (count == 3) {
- count = 0;
- it.remove();
- printList(positions);
- }
- if (!it.hasPrevious()) {
- it.next();
- dir = RIGHT;
- break;
- }
- }
- if (positions.size() == 1) {
- break;
- }
- }
- return 0;
- }
- public static void printList(List<Integer> list) {
- Iterator<Integer> it = list.iterator();
- while (it.hasNext()) {
- System.out.print(it.next() + " ");
- }
- System.out.println();
- }
- public static void main(String[] args) {
- System.out.println("\n\n" + whoIsLucky(5));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement