-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
117 lines (94 loc) · 2.27 KB
/
Test.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
/*
* The Josephus problem solver
* Created by: ahmelq
* github.com/ahmedelq
* October 2017
*/
public class Test {
public static void main(String[] args) {
Scanner keyb= new Scanner(System.in);
System.out.println("Please enter an integer bigger than 1");
int usrInt=0;
while(true) {
try {
usrInt = keyb.nextInt();
if(usrInt>=1)
break;
else
System.out.println("please enter a valid value");
}catch(InputMismatchException e) {
System.out.println("please enter a valid value");
}
}
Soldiers[] n= new Soldiers[usrInt];
fill(n);
killSoldiers(n);
}
public static void kill(Soldiers[] n) {
int firstID =0;
for(int i=0; i<n.length; i++) {
if(!n[i].isKilled()) {
firstID=i;
break;
}
}
for(int x=firstID; x<n.length;x++) {
if(!n[x].isKilled()) {
System.out.println(n[x].getId() + " killed "+ n[getAliveSoldierID(x,n)].getId());
n[getAliveSoldierID(x,n)].setKilled(true);
}
}
}
public static void killSoldiers(Soldiers[] n) {
int c =1;
int nAliveSoldiers= getnAliveSoldiers(n);
while(nAliveSoldiers!=1) {
kill(n);
nAliveSoldiers= getnAliveSoldiers(n);
System.out.println("phase: " + (c++) +"\n");
}
}
public static int getAliveSoldierID(int i, Soldiers e[]) {
i+=1;
int requiredID = -1; // if not found -1
for(;i<e.length;i++) {
if(!e[i].isKilled())
{
requiredID =i;
break;
}
}
if (requiredID==-1) {
for(int x=0;x<e.length;x++) {
if(!e[x].isKilled())
{
requiredID =x;
break;
}
}
}
return requiredID;
}
public static int getnAliveSoldiers(Soldiers e[]) {
int aliveSoldiers=0;
for(int i=0; i<e.length; i++) {
if(!e[i].isKilled())
aliveSoldiers++;
}
return aliveSoldiers;
}
public static void fill(Soldiers e[]) {
int counter=1;
for(int i=0; i<e.length;i++) {
e[i]= new Soldiers(counter++, false);
}
}
public static void print(Soldiers n[]) {
for(int i=0; i<n.length;i++) {
System.out.println("Soldiers ID:" + n[i].getId() +(n[i].isKilled() ? " Killed" : " Alive") );
}
}
}