再帰テストループ


再帰テストループ
import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
ArrayList members = new ArrayList();
for (int i = 1; i <= 20; i++) {
members.add(i);
}
print(members, 3, 0);
}

static void print(List members, int n, int m) {
int r = members.size();
if (r >= n) {
int i = 1;
int thisIndex = i * n - m - 1;
int NextIndex = -1;
while (i<=(r+m)/n) {
System.out.println(members.get(thisIndex));
members.remove(thisIndex);
NextIndex = thisIndex - 1 + n;
i++;
thisIndex = NextIndex;
}

int mo = (r+m) % n;
print(members, n, mo);
}
}

}
}
出力
[quote]3
6
9
12
15
18
1
5
10
14
19
4
11
17
7
16
8
2
[/quote]