https://community.topcoder.com/stat?c=problem_statement&pm=1585&rd=6535
http://www.cnblogs.com/lautsie/p/3261928.html
A busy businessman has a number of equally important tasks which he must accomplish. To decide which of the tasks to perform first, he performs the following operation.
He writes down all his tasks in the form of a circular list, so the first task is adjacent to the last task. He then thinks of a positive number. This number is the random seed, which he calls n. Starting with the first task, he moves clockwise (from element 1 in the list to element 2 in the list and so on), counting from 1 to n. When his count reaches n, he removes that task from the list and starts counting from the next available task. He repeats this procedure until one task remains. It is this last task that he chooses to execute.
Given a String[] list representing the tasks and an int n, return the task which the businessman chooses to execute.
|
public
String getTask(String[] list,
int
n) {
ArrayList<String> al =
new
ArrayList<String>();
for
(
int
i = 0; i < list.length; i++) {
al.add(list[i]);
}
int
len = al.size();
if
(len == 0)
return
null;
int
current = 0;
while
(len != 1) {
int
next = (current + n - 1) % len;
al.
remove
(next);
len = al.size();
current = next % len;
}
return
al.get(0);