标签:
#include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #include<cmath> #include<vector> #include<map> using namespace std; int main(void) { int n; while(cin >> n) { map<int, map<int , int> > tbl; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) tbl[i][j] = 0; int cnt = 1; int cir = 0; while(true) { if(tbl[cir][cir] != 0) break; int siz = n - cir*2; for(int i = 0; i < siz - 1; ++i) tbl[i + cir][cir] = cnt++; for(int j = 0; j < siz - 1; ++j) tbl[cir + siz - 1][cir + j] = cnt++; for(int i = siz - 1; i > 0; --i) tbl[cir + i][cir + siz - 1] = cnt++; for(int j = siz - 1; j > 0; --j) tbl[cir][cir + j] = cnt++; if(tbl[cir][cir] == 0) tbl[cir][cir] = cnt++; ++cir; } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { cout << setw(4) << tbl[i][j]; } cout << endl; } } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_21555605/article/details/46350873