回答編集履歴
2
説明追加
answer
CHANGED
@@ -10,10 +10,11 @@
|
|
10
10
|
|
11
11
|
イメージが湧きにくいかもしれないので,概要をコードで書いてみました.
|
12
12
|
```java
|
13
|
+
//list:じゃんけんするPlayerのリスト,firstRank:この組の最上位が,全体で何位か
|
13
14
|
public static void playJanken(List<Player> list, int firstRank){
|
14
15
|
|
15
|
-
//Playerでじゃんけんを行い,勝ち負けを決定する
|
16
|
+
//listにいるPlayerでじゃんけんを行い,勝ち負けを決定する
|
16
|
-
//(一人の場合には
|
17
|
+
//(一人の場合には,そのPlayerの順位がfirstRankで確定して終了)
|
17
18
|
|
18
19
|
List<Player> winnerList = new ArrayList<Player>();
|
19
20
|
List<Player> loserList = new ArrayList<Player>();
|
@@ -21,8 +22,8 @@
|
|
21
22
|
//勝ったPlayerをwinnerListに,負けたPlayerをloserListに入れる
|
22
23
|
|
23
24
|
//再帰でじゃんけん続行
|
24
|
-
playJanken(winnerList, firstRank);
|
25
|
+
playJanken(winnerList, firstRank); //勝った人でじゃんけん
|
25
|
-
playJanken(loserList, firstRank + winnerList.size());
|
26
|
+
playJanken(loserList, firstRank + winnerList.size()); //負けた人でじゃんけん.勝った人の人数分,順位が下がる
|
26
27
|
}
|
27
28
|
```
|
28
29
|
これで何が起こるかというと,
|
1
再帰のコードを提示
answer
CHANGED
@@ -5,4 +5,30 @@
|
|
5
5
|
また2組に分け,繰り返します.
|
6
6
|
一人だけの組ではじゃんけんせずに終了します.
|
7
7
|
「じゃんけんする」という手続きは人数が変わってるだけで基本的に同じはずなので,
|
8
|
-
こう考えればコードを組みやすくなるのではないでしょうか.
|
8
|
+
こう考えればコードを組みやすくなるのではないでしょうか.
|
9
|
+
---
|
10
|
+
|
11
|
+
イメージが湧きにくいかもしれないので,概要をコードで書いてみました.
|
12
|
+
```java
|
13
|
+
public static void playJanken(List<Player> list, int firstRank){
|
14
|
+
|
15
|
+
//Playerでじゃんけんを行い,勝ち負けを決定する
|
16
|
+
//(一人の場合にはじゃんけんせず終了,そのPlayerの順位が確定する)
|
17
|
+
|
18
|
+
List<Player> winnerList = new ArrayList<Player>();
|
19
|
+
List<Player> loserList = new ArrayList<Player>();
|
20
|
+
|
21
|
+
//勝ったPlayerをwinnerListに,負けたPlayerをloserListに入れる
|
22
|
+
|
23
|
+
//再帰でじゃんけん続行
|
24
|
+
playJanken(winnerList, firstRank);
|
25
|
+
playJanken(loserList, firstRank + winnerList.size());
|
26
|
+
}
|
27
|
+
```
|
28
|
+
これで何が起こるかというと,
|
29
|
+
まず全員でじゃんけんし,勝ち負けで分けます(1).
|
30
|
+
その後,勝ち組でじゃんけんします.この時,新たにplayJankenメソッドが呼ばれます.
|
31
|
+
そしてまた勝ち負けで分かれます(2)が,
|
32
|
+
この(1)のloserListと(2)のloserListは別に管理されていて,互いに干渉しません(上書きされません).
|
33
|
+
メソッドから別のメソッドのローカル変数には干渉できませんよね?それと同じです.
|
34
|
+
これを繰り返して行くといずれ最初の勝ち組の処理が終わるので,負け組の処理に入れます.
|