-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava06.Java
More file actions
250 lines (225 loc) · 4.22 KB
/
Java06.Java
File metadata and controls
250 lines (225 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package java06;
class Java106 {
public static void main(String[] args) {
var l = new P106_08();
l.main_();
}
}
// 본래 코드
// class P106_00 {
// static void f() {
// String a = "Hello World!";
// System.out.println(a);
// f();
// }
// public static void main(String[] args) {
// f();
// }
// }
class P106_00 {
void f() {
String a = "Hello World!";
System.out.println(a);
f();
}
void main_() {
f();
}
}
class P106_01 {
void f(int n) {
if (n == 0) {
return;
}
System.out.println(" Hello, World!");
f(n-1);
}
void main_() {
var n = 5;
f(n);
}
}
class P106_02 {
int f(int n) {
if (n == 0) {
return 1;
}
return n*f(n-1);
}
void main_() {
var n =5;
System.out.println(f(n));
}
}
class P106_03 {
int f(int a, int n) {
if (n == 0) {
return 1;
}
return a*f(a, n-1);
}
void main_() {
var a = 2;
var b = 7;
System.out.println(f(a, b));
}
}
class P106_04 {
int f(int n) {
if (n < 2) {
return n;
}
return f(n-2) + f(n-1);
}
void main_() {
var n = 10;
System.out.println(f(n));
}
}
class P106_04t {
int f(int n, int[] a) {
if (n < 2) {
return n;
}
if (a[n] != 0) {
return a[n];
}
a[n] = f(n-2, a) + f(n-1, a);
return a[n];
}
void main_() {
var n = 45;
System.out.println(f(n, new int[n+1]));
}
}
class P106_05 {
int f(int a, int b) {
if (b == 0) {
return a;
}
return f(b, a%b);
}
void main_(String[] args) {
var a = 1071;
var b = 1029;
System.out.println(f(a,b));
}
}
class P106_06 {
void f(int n, int[] result, int k) {
if (k == n) {
for (var j = 0; j < n; j++) {
System.out.printf("%3d", result[j]);
}
System.out.println();
return;
}
for (var i = 0; i < n; i++) {
result[k] = i ;
f(n, result, k+1);
}
}
void main_() {
var n = 3;
var result = new int[n];
f(n, result, 0);
}
}
class P106_07 {
void f(int n, int[] result, int i, int k) {
if(k == n) {
for (var j = 0; j < n; j++) {
System.out.printf("%3d", result[j]);
}
System.out.println();
return;
}
for (var l = i; l < n; l++) {
result[k] = l;
f(n, result, l, k+1);
}
}
void main_() {
var n = 3;
var result = new int[n];
f(n, result, 0,0);
}
}
class P106_08 {
void f(int n, int m, int[] result, int i, int k) {
if (k == n) {
for (var j = 0; j < n; j++) {
System.out.printf("%3d",result[j]);
}
System.out.println();
return;
}
for (var l = i; l < m; l++) {
result[k] = l;
f(n, m, result, l+1, k+1);
}
}
void main_() {
var n = 3;
var m = 5;
var result = new int[n];
f(n, m, result, 0, 0);
}
}
class P006_09 {
void f(int n, boolean[] used, int[] result, int k) {
if (k == n) {
for (var j = 0; j < n; j++) {
System.out.printf("%3d", result[j]);
}
System.out.println();
return;
}
for (var i = 0; i < n; i++) {
if (used[i]) continue;
used[i] = true;
result[k] = i;
f(n, used, result, k+1);
used[i] = false;
}
}
void main_() {
var n = 3;
var used = new boolean[n];
var result = new int[n];
f(n, used, result, 0);
}
}
class P106_10 {
int f(int n, char from, char middle, char to) {
if (n == 1) {
System.out.printf("Moved %d from %c to %c\n", n, from, to);
return 1;
}
int a = f(n-1, from, to, middle);
System.out.printf("Moved %d from %c to %c\n", n, from, to);
f(n-1, middle, from, to);
return 2*a+1;
}
void main_() {
int n =4;
int count = f(n, 'A', 'B', 'C');
System.out.printf("Moved %d times\n", count);
}
}
class P106_10t {
int f(int n, char from, char middle, char to) {
if (n == 1) {
System.out.printf("Moved %d from %c to %c\n", n, from, to);
return 1;
}
int c = f(n-1, from, to, middle);
System.out.printf("Moved %d from %c to %c\n", n, from, to);
return c + 1 + f(n-1, middle, from, to);
}
void main_() {
int n = 4;
int count = f(n, 'A', 'B', 'C');
System.out.printf("Moved %d times\n", count);
}
}