-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_Spiral_Matrix.cpp
More file actions
58 lines (52 loc) · 925 Bytes
/
Copy pathPrint_Spiral_Matrix.cpp
File metadata and controls
58 lines (52 loc) · 925 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
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
void printSpiralMatrix(int n)
{
if(!n)return;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int t=min(min(n-1-i,i), min(n-1-j,j));
int sum=0;
for(int k=0;k<t;k++)
sum+=((n-k*2)*4-4);
if(i==t)
{
sum+=(j-t+1);
}
else if(i==n-t-1)
{
sum+=((n-t-1-t)*2+(n-t-1-j+1));
}
else if(j==n-t-1)
{
sum+=(n-t-1-t+1)+i-t;
}
else
{
sum+=((n-t-1-t)*3+(n-t-1-i+1));
}
cout<<setw(2)<<(n*n-sum+1)<<" ";
if(j==n-1)cout<<endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
printSpiralMatrix(5);
printSpiralMatrix(1);
printSpiralMatrix(4);
printSpiralMatrix(9);
return 0;
}