프로그래머스/프로그래머스 Lv2
프로그래머스 / 월간 코드 챌린지 시즌1 / 삼각 달팽이도움말
ENUM01
2021. 4. 7. 10:49


|
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
|
#include <string>
#include <vector>
using namespace std;
enum Direction
{
DOWN,
RIGHT,
UP
};
vector<int> solution(int n) {
vector<int> answer;
int array[1001][1001] = { 0, };
int number = 0;
int roofNumber = n;
int index = 0;
int floor = -1;
int direction = DOWN;
while (roofNumber != 0)
{
for (int i = 0; i < roofNumber; i++)
{
switch (direction)
{
case DOWN:
floor++;
break;
case RIGHT:
index++;
break;
case UP:
floor--;
index--;
break;
}
number++;
array[floor][index] = number;
}
roofNumber--;
if (direction == UP)
direction = DOWN;
else
direction++;
}
floor = 1;
index = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
answer.push_back(array[i][j]);
}
return answer;
}
|
cs |