-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndex.java
More file actions
56 lines (56 loc) · 1.34 KB
/
ArrayIndex.java
File metadata and controls
56 lines (56 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int n = sc.nextInt();
Queue<Integer> q = new LinkedList<Integer>();
for (int i=0;i<n;i++)
{
q.offer(i);
}
int count =0;
while(q.size()!=1)
{
if(count!=2)
{
int tem = q.peek();
q.poll();
q.offer(temp);
count++;
}
else
{
q.poll();
count=0;
}
}
System.out.println(q.peek());
}
}
}
---------------------
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n > 1000) {
n = 999;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
list.add(i);
}
int i = 0;
while (list.size() > 1) {
i = (i + 2) % list.size();
list.remove(i);
}
System.out.println(list.get(0));
}
}
}