英文题目:1105 Spiral Matrix
中文题目:1050 螺旋矩阵
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 7 int matrix[10010][110] = {0}; 8 int main() { 9 int N; 10 cin>>N; 11 vector<int> v(N); 12 for(int i = 0; i < N; ++i) cin>>v[i]; 13 sort(v.begin(),v.end(),greater<int>()); 14 int m,n; 15 for(int i = 1; i <= sqrt(N); ++i) { 16 if(N % i == 0) { 17 n = i; 18 m = N/n; 19 } 20 } 21 int num = 0,i = 0,j = -1; 22 while(num < N) { 23 while(j+1 < n && matrix[i][j+1] == 0) 24 matrix[i][++j] = v[num++]; 25 while(i+1 < m && matrix[i+1][j] == 0) 26 matrix[++i][j] = v[num++]; 27 while(j-1 >= 0 && matrix[i][j-1] == 0) 28 matrix[i][--j] = v[num++]; 29 while(i-1 >= 0 && matrix[i-1][j] == 0) 30 matrix[--i][j] = v[num++]; 31 } 32 for(int i = 0; i < m; ++i) { 33 for(int j = 0; j < n; ++j) { 34 if(j > 0) printf(" "); 35 printf("%d",matrix[i][j]); 36 } 37 printf("\n"); 38 } 39 return 0; 40 }