回答編集履歴

2

解説追加

2017/02/20 01:46

投稿

ttyp03
ttyp03

スコア16998

test CHANGED
@@ -8,6 +8,48 @@
8
8
 
9
9
 
10
10
 
11
+ ベストアンサーが決まってしまいましたが、時間ができたので解説しておきます。
12
+
13
+ 他の方のビットを使った処理では、使用する数字の数に限界(32ビットなので32個まで)があるのと、処理のわかりやすさから、次のような配列を用意して処理しています。
14
+
15
+
16
+
17
+
18
+
19
+ A1=1
20
+
21
+ A2=2
22
+
23
+ A3=3
24
+
25
+
26
+
27
+ nums(0,0)=1 nums(1,0)=0
28
+
29
+ nums(0,1)=2 nums(1,1)=0
30
+
31
+ nums(0,2)=3 nums(1,2)=0
32
+
33
+
34
+
35
+ 2次元目は必ず0です。
36
+
37
+ idx配列は、2次元目が0か1のどちらを指すかを管理します。
38
+
39
+ このようにして、idx配列が0と1の全組み合わせを生成します。
40
+
41
+ idx(0)=0,idx(1)=1,idx(2)=0
42
+
43
+ であるなら、
44
+
45
+ nums(0,0)=1,nums(1,1)=0,nums(0,2)=2
46
+
47
+ を参照することになります。
48
+
49
+ numsとidxを使い合計値を算出し、C1と一致すれば式を出力という処理になっています。
50
+
51
+
52
+
11
53
  ```VBA
12
54
 
13
55
  Const MAX = 19

1

修正

2017/02/20 01:46

投稿

ttyp03
ttyp03

スコア16998

test CHANGED
@@ -22,13 +22,11 @@
22
22
 
23
23
 
24
24
 
25
- Dim y As Long
25
+ Dim y As Integer
26
26
 
27
- Dim r As Long
27
+ Dim r As Integer
28
28
 
29
- Dim result As String
30
-
31
- Dim total As Long
29
+ Dim total As Integer
32
30
 
33
31
 
34
32
 
@@ -68,29 +66,7 @@
68
66
 
69
67
  If total = Cells(1, 2).Value Then
70
68
 
71
- result = ""
72
-
73
- For y = 0 To 19
74
-
75
- If idx(y) = 0 Then
76
-
77
- If result = "" Then
78
-
79
- result = "'"
80
-
81
- Else
82
-
83
- result = result & "+"
84
-
85
- End If
86
-
87
- result = result & nums(0, y)
88
-
89
- End If
90
-
91
- Next
92
-
93
- Cells(r, 3).Value = result
69
+ Call OutResult(r)
94
70
 
95
71
  r = r + 1
96
72
 
@@ -156,4 +132,38 @@
156
132
 
157
133
 
158
134
 
135
+ Sub OutResult(r As Integer)
136
+
137
+
138
+
139
+ Dim y As Integer
140
+
141
+ Dim result As String
142
+
143
+
144
+
145
+ result = "'"
146
+
147
+ For y = 0 To 19
148
+
149
+ If idx(y) = 0 Then
150
+
151
+ If result <> "'" Then
152
+
153
+ result = result & "+"
154
+
155
+ End If
156
+
157
+ result = result & nums(0, y)
158
+
159
+ End If
160
+
161
+ Next
162
+
163
+ Cells(r, 3).Value = result
164
+
165
+
166
+
167
+ End Sub
168
+
159
169
  ```