forked from windynight/InterviewPuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintSpiralCubes.cpp
More file actions
61 lines (49 loc) · 888 Bytes
/
Copy pathPrintSpiralCubes.cpp
File metadata and controls
61 lines (49 loc) · 888 Bytes
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
/*
Give an n,
Print Cubes like:
1 - 2 9 - 10
| | |
4 - 3 8 11
| | |
5 - 6 - 7 12
|
16- 15- 14- 13
*/
#include <iostream>
using namespace std;
void printCubes(int n)
{
int num[n][n];
int count = 0;
for (int i = 1; i <= n; i ++) {
if (i % 2 == 0) {
for (int j = 0; j < i; j ++) {
num[j][i - 1] = ++count;
}
for (int j = i - 2; j >= 0; j --) {
num[i - 1][j] = ++count;
}
} else {
for (int j = 0; j < i; j ++) {
num[i - 1][j] = ++count;
}
for (int j = i - 2; j >= 0; j --) {
num[j][i - 1] = ++count;
}
}
}
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n; j ++) {
cout << num[i][j] << '\t';
}
cout << endl;
}
}
int main()
{
int n;
while (cin >> n && n) {
printCubes(n);
}
return 0;
}