回答編集履歴

2

再帰呼出しを使う解を追加

2019/10/11 21:30

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -85,3 +85,71 @@
85
85
  }
86
86
 
87
87
  ```
88
+
89
+ **追記2**
90
+
91
+ 再帰呼出しを使うと、
92
+
93
+ ```C++
94
+
95
+ #include <iostream>
96
+
97
+ #include <algorithm> // copy
98
+
99
+
100
+
101
+ const int N = 4; // N: 1, 2, 3, 4, 5, 6
102
+
103
+ const int M = 256; // NのN乗: 1, 4, 27, 256, 3125, 46656
104
+
105
+ int a[M][N], b[N], k = 0;
106
+
107
+
108
+
109
+ const int A = 1, B = 3, C = 4, D = 6, E = 7, F = 9;
110
+
111
+ const int n[] = { A, B, C, D, E, F };
112
+
113
+
114
+
115
+ void gen(int i)
116
+
117
+ {
118
+
119
+ if (i < N)
120
+
121
+ for (int j = 0; j < N; j++)
122
+
123
+ b[i] = n[j], gen(i + 1);
124
+
125
+ else
126
+
127
+ std::copy(b, b + N, a[k++]);
128
+
129
+ }
130
+
131
+
132
+
133
+ int main(void)
134
+
135
+ {
136
+
137
+ gen(0);
138
+
139
+
140
+
141
+ // 以下、確認
142
+
143
+ for (int i = 0; i < M; i++) {
144
+
145
+ std::cout << "a[" << i << "] = { " << a[i][0];
146
+
147
+ for (int j = 1; j < N; j++) std::cout << ", " << a[i][j];
148
+
149
+ std::cout << " }\n";
150
+
151
+ }
152
+
153
+ }
154
+
155
+ ```

1

別解の追加

2019/10/11 21:30

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -33,3 +33,55 @@
33
33
  }
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ **追記**
40
+
41
+ 6通りの数値まで扱えるようにしてみました。
42
+
43
+ ```C++
44
+
45
+ #include <iostream>
46
+
47
+
48
+
49
+ const int N = 3; // N: 1, 2, 3, 4, 5, 6
50
+
51
+ const int M = 27; // NのN乗: 1, 4, 27, 256, 3125, 46656
52
+
53
+ int a[M][N];
54
+
55
+
56
+
57
+ int main()
58
+
59
+ {
60
+
61
+ int A = 1, B = 3, C = 4, D = 6, E = 7, F = 9;
62
+
63
+ int n[] = { A, B, C, D, E, F };
64
+
65
+
66
+
67
+ for (int i = 0; i < M; i++)
68
+
69
+ for (int v = i, j = N; --j >= 0; v /= N) a[i][j] = n[v % N];
70
+
71
+
72
+
73
+ // 以下、確認
74
+
75
+ for (int i = 0; i < M; i++) {
76
+
77
+ std::cout << "a[" << i << "] = { " << a[i][0];
78
+
79
+ for (int j = 1; j < N; j++) std::cout << ", " << a[i][j];
80
+
81
+ std::cout << " }\n";
82
+
83
+ }
84
+
85
+ }
86
+
87
+ ```