回答編集履歴

2

n、r の説明追記

2022/03/20 20:37

投稿

actorbug
actorbug

スコア2479

test CHANGED
@@ -1,4 +1,5 @@
1
1
  `m`は最大`n`になるのに、`intList`のサイズは最大で`r`にしかならないので、out of rangeになります。
2
+ (`n`、`r`は`combinations`を呼び出した時点の値)
2
3
  素直に`intList.Count`までのループにしましょう。
3
4
 
4
5
  いろいろ修正すると、このような感じでしょうか。

1

表示整形など

2022/03/16 21:02

投稿

actorbug
actorbug

スコア2479

test CHANGED
@@ -6,15 +6,16 @@
6
6
  static void print_combination()
7
7
  {
8
8
  for (int i = 0; i < intList.Count; i++) {
9
- Console.WriteLine(intList[i]);
9
+ Console.Write(intList[i]);
10
10
  }
11
+ Console.WriteLine();
11
12
  }
12
13
 
13
14
  static void comb_sub(int[] list, int n, int r, int m)
14
15
  {
15
16
  if (r == 0)
16
17
  print_combination();
17
- else if (r > 0 && r <= n) {
18
+ else if (r <= n) {
18
19
  comb_sub(list, n - 1, r, m + 1);
19
20
  intList.Add(list[m]);
20
21
  comb_sub(list, n - 1, r - 1, m + 1);