質問編集履歴
26
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -271,3 +271,7 @@
|
|
271
271
|
終了
|
272
272
|
|
273
273
|
このロジックを実現させたいです
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
円盤1がプッシュされた(ピークが円盤1)ならポップにする
|
25
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,110 +116,106 @@
|
|
116
116
|
|
117
117
|
```java
|
118
118
|
|
119
|
+
|
120
|
+
|
119
|
-
|
121
|
+
import java.util.Scanner;
|
122
|
+
|
120
|
-
|
123
|
+
import java.util.Deque;
|
124
|
+
|
125
|
+
import java.util.ArrayDeque;
|
126
|
+
|
127
|
+
class HanoiN {
|
128
|
+
|
121
|
-
static void move(int no, int x, int y){
|
129
|
+
static void move(int no, int x, int y) {
|
130
|
+
|
122
|
-
|
131
|
+
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
132
|
+
|
133
|
+
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
134
|
+
|
135
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
stack.push(no);
|
140
|
+
|
141
|
+
xStack.push(x);
|
142
|
+
|
143
|
+
yStack.push(y);
|
144
|
+
|
145
|
+
while(!stack.isEmpty()){
|
146
|
+
|
123
|
-
|
147
|
+
if(no > 1){
|
148
|
+
|
124
|
-
|
149
|
+
stack.push(--no);
|
150
|
+
|
151
|
+
xStack.push(x);
|
152
|
+
|
125
|
-
|
153
|
+
yStack.push(6-x-y);
|
154
|
+
|
126
|
-
|
155
|
+
continue;
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
while(!stack.isEmpty()){
|
160
|
+
|
161
|
+
no = stack.pop();
|
162
|
+
|
163
|
+
x = xStack.pop();
|
164
|
+
|
165
|
+
y = xStack.pop();
|
166
|
+
|
127
|
-
|
167
|
+
System.out.println("円盤[" + no + "]を" + x + "軸から" + y + "軸へ移動");
|
168
|
+
|
128
|
-
|
169
|
+
do{
|
170
|
+
|
129
|
-
|
171
|
+
if(no > 1){
|
172
|
+
|
130
|
-
|
173
|
+
stack.push(--no);
|
174
|
+
|
175
|
+
xStack.push(6-x-y);
|
176
|
+
|
177
|
+
yStack.push(y);
|
178
|
+
|
179
|
+
x = 6-x-y;
|
180
|
+
|
181
|
+
continue;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}while(stack.peek()!= 1);
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
public static void main(String[] args) {
|
198
|
+
|
199
|
+
Scanner stdIn = new Scanner(System.in);
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
System.out.println("ハノイの塔");
|
204
|
+
|
205
|
+
System.out.print("円盤の枚数:");
|
206
|
+
|
207
|
+
int n = stdIn.nextInt();
|
208
|
+
|
209
|
+
|
210
|
+
|
131
|
-
move(n
|
211
|
+
move(n, 1, 3);
|
212
|
+
|
213
|
+
}
|
132
214
|
|
133
215
|
}
|
134
216
|
|
135
217
|
|
136
218
|
|
137
|
-
import java.util.Scanner;
|
138
|
-
|
139
|
-
import java.util.Deque;
|
140
|
-
|
141
|
-
import java.util.ArrayDeque;
|
142
|
-
|
143
|
-
class HanoiN {
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
//--- 円盤をx軸からy軸へ移動 ---//
|
148
|
-
|
149
|
-
static void move(int no, int x, int y) {
|
150
|
-
|
151
|
-
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
152
|
-
|
153
|
-
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
154
|
-
|
155
|
-
Deque<Integer> stack = new ArrayDeque<Integer>();
|
156
|
-
|
157
|
-
stack.push(1);
|
158
|
-
|
159
|
-
xStack.push(x);
|
160
|
-
|
161
|
-
yStack.push(y);
|
162
|
-
|
163
|
-
while(!stack.isEmpty()){
|
164
|
-
|
165
|
-
if(no > 1){
|
166
|
-
|
167
|
-
stack.push(no-1);
|
168
|
-
|
169
|
-
xStack.push(x);
|
170
|
-
|
171
|
-
yStack.push(6-x-y);
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
no = stack.pop();
|
176
|
-
|
177
|
-
x = xStack.pop();
|
178
|
-
|
179
|
-
y = yStack.pop();
|
180
|
-
|
181
|
-
System.out.println("円盤[" + no + "]を" + x + "軸から" + y + "軸へ移動");
|
182
|
-
|
183
|
-
if(no > 1){
|
184
|
-
|
185
|
-
stack.push(no-1);
|
186
|
-
|
187
|
-
xStack.push(6-x-y);
|
188
|
-
|
189
|
-
yStack.push(y);
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
public static void main(String[] args) {
|
200
|
-
|
201
|
-
Scanner stdIn = new Scanner(System.in);
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
System.out.println("ハノイの塔");
|
206
|
-
|
207
|
-
System.out.print("円盤の枚数:");
|
208
|
-
|
209
|
-
int n = stdIn.nextInt();
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
move(n, 1, 3); // 第1軸に積まれたn枚を第3軸に移動
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
219
|
```
|
224
220
|
|
225
221
|
|
24
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -275,7 +275,3 @@
|
|
275
275
|
終了
|
276
276
|
|
277
277
|
このロジックを実現させたいです
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
円盤1がプッシュされたら、ポップに切り替わる
|
23
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -273,3 +273,9 @@
|
|
273
273
|
(1, 1, 3)ポップ
|
274
274
|
|
275
275
|
終了
|
276
|
+
|
277
|
+
このロジックを実現させたいです
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
円盤1がプッシュされたら、ポップに切り替わる
|
22
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -238,6 +238,8 @@
|
|
238
238
|
|
239
239
|
|
240
240
|
|
241
|
+
move(3, 1, 3);
|
242
|
+
|
241
243
|
開始
|
242
244
|
|
243
245
|
(3, 1, 3)プッシュ
|
21
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -238,8 +238,36 @@
|
|
238
238
|
|
239
239
|
|
240
240
|
|
241
|
+
開始
|
242
|
+
|
243
|
+
(3, 1, 3)プッシュ
|
244
|
+
|
245
|
+
(2, 1, 2)プッシュ
|
246
|
+
|
247
|
+
(1, 1, 3)プッシュ
|
248
|
+
|
241
|
-
|
249
|
+
スタック 入り口 (1, 1, 3)(2, 1, 2)(3, 1, 3) 底
|
250
|
+
|
242
|
-
|
251
|
+
(1, 1, 3)ポップ
|
252
|
+
|
253
|
+
(2, 1, 2)ポップ
|
254
|
+
|
243
|
-
|
255
|
+
(1, 3, 2)プッシュ
|
256
|
+
|
244
|
-
|
257
|
+
(1, 3, 2)ポップ
|
258
|
+
|
259
|
+
(3, 1, 3)ポップ
|
260
|
+
|
245
|
-
|
261
|
+
(2, 2, 3)プッシュ
|
262
|
+
|
263
|
+
(1, 2, 1)プッシュ
|
264
|
+
|
265
|
+
(1, 2, 1)ポップ
|
266
|
+
|
267
|
+
(2, 2, 3)ポップ
|
268
|
+
|
269
|
+
(1, 1, 3)プッシュ
|
270
|
+
|
271
|
+
(1, 1, 3)ポップ
|
272
|
+
|
273
|
+
終了
|
20
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -96,210 +96,132 @@
|
|
96
96
|
|
97
97
|
|
98
98
|
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
###発生している問題・エラーメッセージ
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
エラーメッセージ
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
|
99
|
-
|
115
|
+
###該当のソースコード
|
116
|
+
|
100
|
-
|
117
|
+
```java
|
118
|
+
|
101
|
-
|
119
|
+
//再帰的メソッドmove
|
102
|
-
|
103
|
-
|
120
|
+
|
104
|
-
|
105
|
-
|
121
|
+
static void move(int no, int x, int y){
|
106
|
-
|
122
|
+
|
107
|
-
if(no >
|
123
|
+
if(no > 0)
|
108
|
-
|
109
|
-
|
124
|
+
|
110
|
-
|
111
|
-
xStack.push(x);
|
112
|
-
|
113
|
-
|
125
|
+
move(no-1, x, 6-x-y);
|
114
|
-
|
115
|
-
|
126
|
+
|
116
|
-
|
117
|
-
no = stack.pop();
|
118
|
-
|
119
|
-
x = xStack.pop();
|
120
|
-
|
121
|
-
y = yStack.pop();
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
System.out.println("円盤[" + no + "]を" + x + "軸から" + y + "軸へ移動");
|
127
|
+
System.out.println("円盤[" + no + +"]を" + x + "軸から" + y + "軸へ移動");
|
126
|
-
|
128
|
+
|
127
|
-
if(no > 1)
|
129
|
+
if(no > 1)
|
128
|
-
|
129
|
-
|
130
|
+
|
130
|
-
|
131
|
-
|
131
|
+
move(no-1, 6-x-y, y);
|
132
|
-
|
133
|
-
stack.push(y)
|
134
132
|
|
135
133
|
}
|
136
134
|
|
135
|
+
|
136
|
+
|
137
|
+
import java.util.Scanner;
|
138
|
+
|
139
|
+
import java.util.Deque;
|
140
|
+
|
141
|
+
import java.util.ArrayDeque;
|
142
|
+
|
143
|
+
class HanoiN {
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
//--- 円盤をx軸からy軸へ移動 ---//
|
148
|
+
|
149
|
+
static void move(int no, int x, int y) {
|
150
|
+
|
151
|
+
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
152
|
+
|
153
|
+
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
154
|
+
|
155
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
156
|
+
|
157
|
+
stack.push(1);
|
158
|
+
|
159
|
+
xStack.push(x);
|
160
|
+
|
161
|
+
yStack.push(y);
|
162
|
+
|
163
|
+
while(!stack.isEmpty()){
|
164
|
+
|
165
|
+
if(no > 1){
|
166
|
+
|
167
|
+
stack.push(no-1);
|
168
|
+
|
169
|
+
xStack.push(x);
|
170
|
+
|
171
|
+
yStack.push(6-x-y);
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
no = stack.pop();
|
176
|
+
|
177
|
+
x = xStack.pop();
|
178
|
+
|
179
|
+
y = yStack.pop();
|
180
|
+
|
181
|
+
System.out.println("円盤[" + no + "]を" + x + "軸から" + y + "軸へ移動");
|
182
|
+
|
183
|
+
if(no > 1){
|
184
|
+
|
185
|
+
stack.push(no-1);
|
186
|
+
|
187
|
+
xStack.push(6-x-y);
|
188
|
+
|
189
|
+
yStack.push(y);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public static void main(String[] args) {
|
200
|
+
|
201
|
+
Scanner stdIn = new Scanner(System.in);
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
System.out.println("ハノイの塔");
|
206
|
+
|
207
|
+
System.out.print("円盤の枚数:");
|
208
|
+
|
209
|
+
int n = stdIn.nextInt();
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
move(n, 1, 3); // 第1軸に積まれたn枚を第3軸に移動
|
214
|
+
|
215
|
+
}
|
216
|
+
|
137
217
|
}
|
138
218
|
|
139
219
|
|
140
220
|
|
141
221
|
|
142
222
|
|
143
|
-
|
144
|
-
|
145
|
-
###発生している問題・エラーメッセージ
|
146
|
-
|
147
|
-
|
148
|
-
|
149
223
|
```
|
150
224
|
|
151
|
-
エラーメッセージ
|
152
|
-
|
153
|
-
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
###該当のソースコード
|
158
|
-
|
159
|
-
```java
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
//メソッドmoveは再帰的にはこう書きます
|
164
|
-
|
165
|
-
static void move(int no, int x, int y){
|
166
|
-
|
167
|
-
if(no > 1)
|
168
|
-
|
169
|
-
move(no-1, x, 6-x-y);
|
170
|
-
|
171
|
-
System.out.printf("円盤[%d]を%d軸から%d軸へ移動", no, x, y);
|
172
|
-
|
173
|
-
if(no > 1)
|
174
|
-
|
175
|
-
move(no-1, 6-x-y, y);
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
import java.util.Scanner;
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
class HanoiN {
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
//--- 円盤をx軸からy軸へ移動 ---//
|
190
|
-
|
191
|
-
static void move(int no, int x, int y) {
|
192
|
-
|
193
|
-
int[] xstk = new int[100];
|
194
|
-
|
195
|
-
int[] ystk = new int[100];
|
196
|
-
|
197
|
-
int[] sstk = new int[100]; // スタック
|
198
|
-
|
199
|
-
int ptr = 0; // スタックポインタ
|
200
|
-
|
201
|
-
int sw = 0;
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
while (true) {
|
206
|
-
|
207
|
-
if (sw == 0 && no > 1) {
|
208
|
-
|
209
|
-
xstk[ptr] = x; // xの値をプッシュ
|
210
|
-
|
211
|
-
ystk[ptr] = y; // yの値をプッシュ
|
212
|
-
|
213
|
-
sstk[ptr] = sw; // swの値をプッシュ
|
214
|
-
|
215
|
-
ptr++;
|
216
|
-
|
217
|
-
no = no - 1;
|
218
|
-
|
219
|
-
y = 6 - x - y;
|
220
|
-
|
221
|
-
continue;
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
System.out.printf("円盤[%d]を%d軸から%d軸へ移動\n", no, x, y);
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
if (sw == 1 && no > 1) {
|
232
|
-
|
233
|
-
xstk[ptr] = x; // xの値をプッシュ
|
234
|
-
|
235
|
-
ystk[ptr] = y; // yの値をプッシュ
|
236
|
-
|
237
|
-
sstk[ptr] = sw; // swの値をプッシュ
|
238
|
-
|
239
|
-
ptr++;
|
240
|
-
|
241
|
-
no = no - 1;
|
242
|
-
|
243
|
-
x = 6 - x - y;
|
244
|
-
|
245
|
-
sw++;
|
246
|
-
|
247
|
-
if (sw == 2) sw = 0;
|
248
|
-
|
249
|
-
continue;
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
do {
|
254
|
-
|
255
|
-
if (ptr == 0) // スタックが空になったら
|
256
|
-
|
257
|
-
return;
|
258
|
-
|
259
|
-
ptr--;
|
260
|
-
|
261
|
-
x = xstk[ptr]; // 値を保存していたxをポップ
|
262
|
-
|
263
|
-
y = ystk[ptr]; // 値を保存していたyをポップ
|
264
|
-
|
265
|
-
sw = sstk[ptr] + 1; // 値を保存していたswをポップ
|
266
|
-
|
267
|
-
no++;
|
268
|
-
|
269
|
-
} while (sw == 2);
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
public static void main(String[] args) {
|
278
|
-
|
279
|
-
Scanner stdIn = new Scanner(System.in);
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
System.out.println("ハノイの塔");
|
284
|
-
|
285
|
-
System.out.print("円盤の枚数:");
|
286
|
-
|
287
|
-
int n = stdIn.nextInt();
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
move(n, 1, 3); // 第1軸に積まれたn枚を第3軸に移動
|
292
|
-
|
293
|
-
}
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
```
|
302
|
-
|
303
225
|
|
304
226
|
|
305
227
|
###試したこと
|
19
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -114,7 +114,15 @@
|
|
114
114
|
|
115
115
|
}
|
116
116
|
|
117
|
+
no = stack.pop();
|
118
|
+
|
119
|
+
x = xStack.pop();
|
120
|
+
|
121
|
+
y = yStack.pop();
|
122
|
+
|
123
|
+
|
124
|
+
|
117
|
-
System.out.println("円盤[" +
|
125
|
+
System.out.println("円盤[" + no + "]を" + x + "軸から" + y + "軸へ移動");
|
118
126
|
|
119
127
|
if(no > 1){
|
120
128
|
|
@@ -126,7 +134,7 @@
|
|
126
134
|
|
127
135
|
}
|
128
136
|
|
129
|
-
|
137
|
+
}
|
130
138
|
|
131
139
|
|
132
140
|
|
18
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
ハノイの塔を
|
5
|
+
ハノイの塔を
|
6
6
|
|
7
7
|
スタックを使って書くにはどうすればいいか
|
8
8
|
|
@@ -84,6 +84,56 @@
|
|
84
84
|
|
85
85
|
|
86
86
|
|
87
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
88
|
+
|
89
|
+
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
90
|
+
|
91
|
+
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
move(3, 1, 3)
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
stack.push(no);
|
100
|
+
|
101
|
+
xStack.push(x);
|
102
|
+
|
103
|
+
yStack.push(y);
|
104
|
+
|
105
|
+
while(!stack.isEmpty()){
|
106
|
+
|
107
|
+
if(no > 1){
|
108
|
+
|
109
|
+
stack.push(no-1);
|
110
|
+
|
111
|
+
xStack.push(x);
|
112
|
+
|
113
|
+
yStack.push(6-x-y);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
System.out.println("円盤[" + stack.pop() + "]を" + xStack.pop() + "軸から" + yStack.pop() + "軸へ移動");
|
118
|
+
|
119
|
+
if(no > 1){
|
120
|
+
|
121
|
+
stack.push(no-1);
|
122
|
+
|
123
|
+
stack.push(6-x-y);
|
124
|
+
|
125
|
+
stack.push(y)
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
87
137
|
###発生している問題・エラーメッセージ
|
88
138
|
|
89
139
|
|
17
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,17 +20,13 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
スタックならスタックを使って書けば分かりやすいはずです
|
24
|
-
|
25
|
-
|
26
|
-
|
27
23
|
3ステップ
|
28
24
|
|
29
|
-
開始軸から中間軸へno-1枚移動する
|
25
|
+
●開始軸から中間軸へno-1枚移動する
|
30
|
-
|
26
|
+
|
31
|
-
円盤noを開始軸から目的軸へ移動
|
27
|
+
●円盤noを開始軸から目的軸へ移動
|
32
|
-
|
28
|
+
|
33
|
-
中間軸から目的軸へno-1枚移動する
|
29
|
+
●中間軸から目的軸へno-1枚移動する
|
34
30
|
|
35
31
|
|
36
32
|
|
@@ -40,7 +36,7 @@
|
|
40
36
|
|
41
37
|
これが
|
42
38
|
|
43
|
-
move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
39
|
+
●move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
44
40
|
|
45
41
|
(1, 1, 3)円盤1を1軸から3軸へ移動
|
46
42
|
|
@@ -50,11 +46,11 @@
|
|
50
46
|
|
51
47
|
|
52
48
|
|
53
|
-
(3, 1, 3)円盤3を1軸から3軸へ移動
|
49
|
+
●(3, 1, 3)円盤3を1軸から3軸へ移動
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
|
51
|
+
|
52
|
+
|
57
|
-
move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
53
|
+
●move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
58
54
|
|
59
55
|
(1, 2, 1)円盤1を2軸から1軸へ移動
|
60
56
|
|
@@ -66,6 +62,12 @@
|
|
66
62
|
|
67
63
|
|
68
64
|
|
65
|
+
この順で
|
66
|
+
|
67
|
+
取り出し出されるスタックを書きたい
|
68
|
+
|
69
|
+
|
70
|
+
|
69
71
|
これをpushとpopを使って書きたい
|
70
72
|
|
71
73
|
|
@@ -76,12 +78,6 @@
|
|
76
78
|
|
77
79
|
|
78
80
|
|
79
|
-
少なくてもこの配列をポインタで直に扱うようなものは変えたいと思います。
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
84
|
-
|
85
81
|
while(!stack.isEmpty())文を使いたいです。
|
86
82
|
|
87
83
|
|
@@ -104,6 +100,26 @@
|
|
104
100
|
|
105
101
|
```java
|
106
102
|
|
103
|
+
|
104
|
+
|
105
|
+
//メソッドmoveは再帰的にはこう書きます
|
106
|
+
|
107
|
+
static void move(int no, int x, int y){
|
108
|
+
|
109
|
+
if(no > 1)
|
110
|
+
|
111
|
+
move(no-1, x, 6-x-y);
|
112
|
+
|
113
|
+
System.out.printf("円盤[%d]を%d軸から%d軸へ移動", no, x, y);
|
114
|
+
|
115
|
+
if(no > 1)
|
116
|
+
|
117
|
+
move(no-1, 6-x-y, y);
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
107
123
|
import java.util.Scanner;
|
108
124
|
|
109
125
|
|
@@ -222,21 +238,7 @@
|
|
222
238
|
|
223
239
|
|
224
240
|
|
225
|
-
|
241
|
+
|
226
|
-
|
227
|
-
static void move(int no, int x, int y){
|
228
|
-
|
229
|
-
if(no > 1)
|
230
|
-
|
231
|
-
move(no-1, x, 6-x-y);
|
232
|
-
|
233
|
-
System.out.printf("円盤[%d]を%d軸から%d軸へ移動", no, x, y);
|
234
|
-
|
235
|
-
if(no > 1)
|
236
|
-
|
237
|
-
move(no-1, 6-x-y, y);
|
238
|
-
|
239
|
-
}
|
240
242
|
|
241
243
|
```
|
242
244
|
|
16
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
n = 3のとき
|
37
|
+
no = 3のとき
|
38
38
|
|
39
39
|
move(3, 1, 3)
|
40
40
|
|
@@ -42,11 +42,11 @@
|
|
42
42
|
|
43
43
|
move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
44
44
|
|
45
|
-
(1, 1, 3)円盤1を1軸から3軸へ移動
|
45
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
46
|
-
|
46
|
+
|
47
|
-
(2, 1, 2)円盤2を1軸から2軸へ移動
|
47
|
+
(2, 1, 2)円盤2を1軸から2軸へ移動
|
48
|
-
|
48
|
+
|
49
|
-
(1, 3, 2)円盤1を3軸から2軸へ移動
|
49
|
+
(1, 3, 2)円盤1を3軸から2軸へ移動
|
50
50
|
|
51
51
|
|
52
52
|
|
@@ -56,11 +56,11 @@
|
|
56
56
|
|
57
57
|
move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
58
58
|
|
59
|
-
(1, 2, 1)円盤1を2軸から1軸へ移動
|
59
|
+
(1, 2, 1)円盤1を2軸から1軸へ移動
|
60
|
-
|
60
|
+
|
61
|
-
(2, 2, 3)円盤2を2軸から3軸へ移動
|
61
|
+
(2, 2, 3)円盤2を2軸から3軸へ移動
|
62
|
-
|
62
|
+
|
63
|
-
(1, 1, 3)円盤1を1軸から3軸へ移動
|
63
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
64
64
|
|
65
65
|
これで完了
|
66
66
|
|
15
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,22 +20,56 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
このソースコードのwhile(sw == 2)になる時は
|
24
|
-
|
25
|
-
sw が1の時の
|
26
|
-
|
27
|
-
sstk[ptr] = sw;が1を格納した後にsstk[ptr]から
|
28
|
-
|
29
|
-
取り出されるsw = sstk[prt]+1;ときにですよね
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
ポインタを使っていて読みにくいなという感じがします
|
34
|
-
|
35
23
|
スタックならスタックを使って書けば分かりやすいはずです
|
36
24
|
|
37
25
|
|
38
26
|
|
27
|
+
3ステップ
|
28
|
+
|
29
|
+
開始軸から中間軸へno-1枚移動する
|
30
|
+
|
31
|
+
円盤noを開始軸から目的軸へ移動
|
32
|
+
|
33
|
+
中間軸から目的軸へno-1枚移動する
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
n = 3のとき
|
38
|
+
|
39
|
+
move(3, 1, 3)
|
40
|
+
|
41
|
+
これが
|
42
|
+
|
43
|
+
move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
44
|
+
|
45
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
46
|
+
|
47
|
+
(2, 1, 2)円盤2を1軸から2軸へ移動
|
48
|
+
|
49
|
+
(1, 3, 2)円盤1を3軸から2軸へ移動
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
(3, 1, 3)円盤3を1軸から3軸へ移動
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
58
|
+
|
59
|
+
(1, 2, 1)円盤1を2軸から1軸へ移動
|
60
|
+
|
61
|
+
(2, 2, 3)円盤2を2軸から3軸へ移動
|
62
|
+
|
63
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
64
|
+
|
65
|
+
これで完了
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
これをpushとpopを使って書きたい
|
70
|
+
|
71
|
+
|
72
|
+
|
39
73
|
java.util.ArrayDequeでpopとpushを使ってスタックとして書くとどうなるのでしょうか
|
40
74
|
|
41
75
|
Deque<Integer>
|
@@ -227,45 +261,3 @@
|
|
227
261
|
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
228
262
|
|
229
263
|
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
3ステップ
|
234
|
-
|
235
|
-
開始軸から中間軸へno-1枚移動する
|
236
|
-
|
237
|
-
円盤noを開始軸から目的軸へ移動
|
238
|
-
|
239
|
-
中間軸から目的軸へno-1枚移動する
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
n = 3のとき
|
244
|
-
|
245
|
-
move(3, 1, 3)
|
246
|
-
|
247
|
-
これが
|
248
|
-
|
249
|
-
move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
250
|
-
|
251
|
-
(1, 1, 3)円盤1を1軸から3軸へ移動
|
252
|
-
|
253
|
-
(2, 1, 2)円盤2を1軸から2軸へ移動
|
254
|
-
|
255
|
-
(1, 3, 2)円盤1を3軸から2軸へ移動
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
(3, 1, 3)円盤3を1軸から3軸へ移動
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
264
|
-
|
265
|
-
(1, 2, 1)円盤1を2軸から1軸へ移動
|
266
|
-
|
267
|
-
(2, 2, 3)円盤2を2軸から3軸へ移動
|
268
|
-
|
269
|
-
(1, 1, 3)円盤1を1軸から3軸へ移動
|
270
|
-
|
271
|
-
これで完了
|
14
說明
test
CHANGED
File without changes
|
test
CHANGED
@@ -230,26 +230,42 @@
|
|
230
230
|
|
231
231
|
|
232
232
|
|
233
|
+
3ステップ
|
234
|
+
|
235
|
+
開始軸から中間軸へno-1枚移動する
|
236
|
+
|
237
|
+
円盤noを開始軸から目的軸へ移動
|
238
|
+
|
239
|
+
中間軸から目的軸へno-1枚移動する
|
240
|
+
|
241
|
+
|
242
|
+
|
233
243
|
n = 3のとき
|
234
244
|
|
235
|
-
(3, 1, 3)
|
245
|
+
move(3, 1, 3)
|
236
246
|
|
237
247
|
これが
|
238
248
|
|
249
|
+
move(2, 1, 2):2枚の円盤を1軸から2軸へ移動する
|
250
|
+
|
251
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
252
|
+
|
239
|
-
(2, 1, 2)
|
253
|
+
(2, 1, 2)円盤2を1軸から2軸へ移動
|
254
|
+
|
240
|
-
|
255
|
+
(1, 3, 2)円盤1を3軸から2軸へ移動
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
(3, 1, 3)円盤3を1軸から3軸へ移動
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
move(2, 2, 3):2枚の円盤を2軸から3軸へ移動
|
264
|
+
|
265
|
+
(1, 2, 1)円盤1を2軸から1軸へ移動
|
266
|
+
|
267
|
+
(2, 2, 3)円盤2を2軸から3軸へ移動
|
268
|
+
|
241
|
-
(1, 1, 3)
|
269
|
+
(1, 1, 3)円盤1を1軸から3軸へ移動
|
242
|
-
|
243
|
-
(2, 1, 2)
|
244
|
-
|
245
|
-
(1, 3, 2)
|
246
|
-
|
247
|
-
(3, 1, 3)
|
248
|
-
|
249
|
-
(1, 2, 1)
|
250
|
-
|
251
|
-
(2, 2, 3)
|
252
|
-
|
253
|
-
(1, 1, 3)
|
254
270
|
|
255
271
|
これで完了
|
13
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -219,3 +219,37 @@
|
|
219
219
|
Deque<Integer>
|
220
220
|
|
221
221
|
https://teratail.com/questions/107057
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
226
|
+
|
227
|
+
Deque<Integer> xStack = new ArrayDeque<Integer>();
|
228
|
+
|
229
|
+
Deque<Integer> yStack = new ArrayDeque<Integer>();
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
n = 3のとき
|
234
|
+
|
235
|
+
(3, 1, 3):3枚の円盤を1軸から3軸へ移動する
|
236
|
+
|
237
|
+
これが
|
238
|
+
|
239
|
+
(2, 1, 2)
|
240
|
+
|
241
|
+
(1, 1, 3)
|
242
|
+
|
243
|
+
(2, 1, 2)
|
244
|
+
|
245
|
+
(1, 3, 2)
|
246
|
+
|
247
|
+
(3, 1, 3)
|
248
|
+
|
249
|
+
(1, 2, 1)
|
250
|
+
|
251
|
+
(2, 2, 3)
|
252
|
+
|
253
|
+
(1, 1, 3)
|
254
|
+
|
255
|
+
これで完了
|
12
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,7 +116,7 @@
|
|
116
116
|
|
117
117
|
|
118
118
|
|
119
|
-
System.out.printf("[%d]を%d軸から%d軸へ移動\n", no, x, y);
|
119
|
+
System.out.printf("円盤[%d]を%d軸から%d軸へ移動\n", no, x, y);
|
120
120
|
|
121
121
|
|
122
122
|
|
@@ -196,7 +196,7 @@
|
|
196
196
|
|
197
197
|
move(no-1, x, 6-x-y);
|
198
198
|
|
199
|
-
System.out.printf("[%d]を%d軸から%d軸へ移動", no, x, y);
|
199
|
+
System.out.printf("円盤[%d]を%d軸から%d軸へ移動", no, x, y);
|
200
200
|
|
201
201
|
if(no > 1)
|
202
202
|
|
11
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -190,7 +190,7 @@
|
|
190
190
|
|
191
191
|
//メソッドmoveは再帰的にはこう書きます
|
192
192
|
|
193
|
-
static void move(int n
|
193
|
+
static void move(int no, int x, int y){
|
194
194
|
|
195
195
|
if(no > 1)
|
196
196
|
|
10
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -186,6 +186,24 @@
|
|
186
186
|
|
187
187
|
}
|
188
188
|
|
189
|
+
|
190
|
+
|
191
|
+
//メソッドmoveは再帰的にはこう書きます
|
192
|
+
|
193
|
+
static void move(int n0, int x, int y){
|
194
|
+
|
195
|
+
if(no > 1)
|
196
|
+
|
197
|
+
move(no-1, x, 6-x-y);
|
198
|
+
|
199
|
+
System.out.printf("[%d]を%d軸から%d軸へ移動", no, x, y);
|
200
|
+
|
201
|
+
if(no > 1)
|
202
|
+
|
203
|
+
move(no-1, 6-x-y, y);
|
204
|
+
|
205
|
+
}
|
206
|
+
|
189
207
|
```
|
190
208
|
|
191
209
|
|
9
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
ハノイの塔を非再帰的に書く
|
6
|
+
|
7
|
+
スタックを使って書くにはどうすればいいか
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
Deque<Integer> xstack = new ArrayDeque<Integer>();
|
12
|
+
|
13
|
+
Deque<Integer> ystack = new ArrayDeque<Integer>();
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
円盤の数字を格納するスタックも必要ですね
|
18
|
+
|
19
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
20
|
+
|
21
|
+
|
22
|
+
|
5
23
|
このソースコードのwhile(sw == 2)になる時は
|
6
24
|
|
7
25
|
sw が1の時の
|
@@ -34,18 +52,6 @@
|
|
34
52
|
|
35
53
|
|
36
54
|
|
37
|
-
Deque<Integer> xstack = new ArrayDeque<Integer>();
|
38
|
-
|
39
|
-
Deque<Integer> ystack = new ArrayDeque<Integer>();
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
円盤の数字を格納するスタックも必要ですね
|
44
|
-
|
45
|
-
Deque<Integer> stack = new ArrayDeque<Integer>();
|
46
|
-
|
47
|
-
|
48
|
-
|
49
55
|
|
50
56
|
|
51
57
|
###発生している問題・エラーメッセージ
|
8
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
|
31
31
|
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
32
32
|
|
33
|
-
while(!
|
33
|
+
while(!stack.isEmpty())文を使いたいです。
|
34
34
|
|
35
35
|
|
36
36
|
|
7
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,13 +30,19 @@
|
|
30
30
|
|
31
31
|
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
32
32
|
|
33
|
-
while(!xstack.isEmpty())文を使いたいです。
|
33
|
+
while(!x\stack.isEmpty())文を使いたいです。
|
34
34
|
|
35
35
|
|
36
36
|
|
37
37
|
Deque<Integer> xstack = new ArrayDeque<Integer>();
|
38
38
|
|
39
39
|
Deque<Integer> ystack = new ArrayDeque<Integer>();
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
円盤の数字を格納するスタックも必要ですね
|
44
|
+
|
45
|
+
Deque<Integer> stack = new ArrayDeque<Integer>();
|
40
46
|
|
41
47
|
|
42
48
|
|
6
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
|
31
31
|
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
32
32
|
|
33
|
-
while(!xstack.isEmpty())
|
33
|
+
while(!xstack.isEmpty())文を使いたいです。
|
34
34
|
|
35
35
|
|
36
36
|
|
5
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,6 +29,14 @@
|
|
29
29
|
|
30
30
|
|
31
31
|
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
32
|
+
|
33
|
+
while(!xstack.isEmpty())分を使いたいです。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
Deque<Integer> xstack = new ArrayDeque<Integer>();
|
38
|
+
|
39
|
+
Deque<Integer> ystack = new ArrayDeque<Integer>();
|
32
40
|
|
33
41
|
|
34
42
|
|
4
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,6 +25,10 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
少なくてもこの配列をポインタで直に扱うようなものは変えたいと思います。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
スタックを使うにも、sw や sstkは使わないで実現したいです。
|
28
32
|
|
29
33
|
|
30
34
|
|
3
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,6 +19,8 @@
|
|
19
19
|
|
20
20
|
|
21
21
|
java.util.ArrayDequeでpopとpushを使ってスタックとして書くとどうなるのでしょうか
|
22
|
+
|
23
|
+
Deque<Integer>
|
22
24
|
|
23
25
|
|
24
26
|
|
@@ -172,6 +174,6 @@
|
|
172
174
|
|
173
175
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
174
176
|
|
175
|
-
|
177
|
+
Deque<Integer>
|
176
178
|
|
177
179
|
https://teratail.com/questions/107057
|
2
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -172,4 +172,6 @@
|
|
172
172
|
|
173
173
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
174
174
|
|
175
|
+
|
176
|
+
|
175
|
-
|
177
|
+
https://teratail.com/questions/107057
|
1
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,6 +19,10 @@
|
|
19
19
|
|
20
20
|
|
21
21
|
java.util.ArrayDequeでpopとpushを使ってスタックとして書くとどうなるのでしょうか
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
少なくてもこの配列をポインタで直に扱うようなものは変えたいと思います。
|
22
26
|
|
23
27
|
|
24
28
|
|