-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge4.kt
79 lines (70 loc) · 1.49 KB
/
challenge4.kt
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
fun main() {
generateSpiralMatrix(3)
}
fun generateSpiralMatrix(n: Int) {
var m = Array(n) { IntArray(n) { 0 } }
var f = n
var ini = 0
var d = 0
var ii = 0
var jj = 0
var test = true
while (test) {
if (d >= f-1) {
break
}
if (ii == d && jj == d) {
for (j in d..f - 1) {
m[ii][jj] = ini
ini++
jj = j
}
} else if (jj == f - 1 && ii == d) {
for (i in d..f - 1) {
m[ii][jj] = ini
ini++
ii = i
}
} else if (jj == f-1 && ii == f-1) {
for (j in f-1 downTo d) {
m[ii][jj] = ini
ini++
jj = j
}
} else if (ii == f-1 && jj == d) {
d++
f--
for (i in f-1 downTo d) {
m[ii][jj] = ini
ini++
ii = i
}
}
for (row in m) {
println(row.contentToString())
}
}
// for (row in m) {
// println(row.contentToString())
// }
// var i = 0
// var j = 0
// var ini = 1
// var test = true
// while (test) {
// m[i][j] = ini
// ini++
// if (j == f && i < f ) {
// i++
// } else if (j == f && i == f ) {
//
// j--
// }
// else if(){
// i--
//
// }
// else if(i==d&&j==d-1){}
//
// }
}