-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral
120 lines (110 loc) · 3.74 KB
/
Spiral
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication102
{
class Program
{
static void Main(string[] args)
{
int row = 50;
int col = 50;
int[,] arr = new int[row, col];
int x = row / 2, y = col / 2;
int count = 1;
int count2 = 1;
bool up = true;
bool down = false;
bool control = true;
int step = 4;
int step2 = 3;
arr[x, y] = 1; // centr
arr[x += step2, y += step2] = ++count;
arr[x += step2, y -= step2] = ++count;
//arr[x, y -= 2] = ++count;
for(int j = 0 ; j <5; j++ )
{
if(up)
arr[x, y -= step] = ++count;
for(int i = 0; i < count2 && x > 0 && x <= row ;i++)
{
if (up)
{
if (control)
{
arr[x -= step2, y -= step2] = ++count;
if (i + 1 == count2)
{
arr[x -= step2, y] = ++count;
control = false;
i = -1;
continue;
}
}
if (control == false)
{
arr[x -= step2, y += step2] = ++count;
if(i+1 == count2)
{
up = false;
down = true;
count2++;
break;
}
// arr[x -= 2, y += 2] = ++count;
}
}
if(down)
{
if (!control)
{
arr[x += step2, y += step2] = ++count;
if(i+1 == count2)
{
// arr[x += step2, y] = ++count;
control = true;
i = -1;
continue;
}
}
if(control)
{
arr[x += step2, y -= step2] = ++count;
if(i+1 == count2)
{
down = false;
up = true;
break;
}
// arr[x += step2, y -= step2] = ++count;
}
}
}
if(down)
arr[x, y+= step] = ++count;
step++;
}
for(int i =0; i <row;i++)
{
for(int j =0;j<col;j++)
{
if (arr[i, j] == 0)
{
char a = ' ';
Console.Write(a);
}
else
{
if(arr[i,j].ToString().Length == 1)
Console.Write(arr[i, j] + " ");
else
Console.Write(arr[i, j]);
}
}
Console.WriteLine();
}
}
}
}