forked from Guarav/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPD.cpp
70 lines (60 loc) · 1.41 KB
/
PD.cpp
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
#include<iostream>
#include<stdio.h>
using namespace std;
void display(int n)
{
int limit = n * n;
int leftIndex = 0;
int rightIndex = n - 1;
int topIndex = 0;
int bottomIndex = n - 1;
int matrix[n][n];
int num = 1;
while(num <= limit )
{
for( int i = leftIndex; i <= rightIndex; ++i )
{
matrix[topIndex][i] = num++;
}
for(int i = topIndex + 1; i <= bottomIndex; ++i)
{
matrix[i][rightIndex] = num++;
}
for(int i = rightIndex - 1; i >= leftIndex; --i)
{
matrix[bottomIndex][i] = num++;
}
for(int i = bottomIndex - 1; i > topIndex; --i)
{
matrix[i][leftIndex] = num++;
}
leftIndex++;
rightIndex--;
topIndex++;
bottomIndex--;
}
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n ; ++j)
{
if( j != n-1 )
printf("%d\t", matrix[i][j]);
else
printf("%d",matrix[i][j]);
}
//if( i != n-1 )
printf("\n");
}
}
int main()
{
int n;
scanf("%d", &n);
/*
if( n == 1 )
printf("1\n");
else
*/
display(n);
return 0;
}