AC【螺旋矩阵】三部曲:1、[ 找到 m、n ] 2、[ 开二维数组填充矩阵 ] 3、[ 输出矩阵 ]
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool cmp(int a, int b) { return a > b; } 5 6 int G[10000][10000]; 7 8 int main() 9 { 10 int N, m, n, x = 10000 ; cin >> N; //输入第一行 11 12 int matrix[N+1]; 13 for(int i = 0; i < N; i++) cin >> matrix[i];//输入第二行 14 15 for(int i = 1; i <= N; i++)//找出[m n] 16 {//[m × n must be equal to N; m≥n; and m?n is the minimum of all the possible values.] 17 for(int j = 1; j <= N; j++) 18 { 19 if(i * j == N && i >= j) 20 { 21 if( (i - j) < x && (i - j) >= 0) 22 { 23 x = (i - j); m = i; n = j; 24 } 25 } 26 } 27 } 28 sort(matrix, matrix + N, cmp);//递减排序 29 30 int num = 0, round = 0; 31 while(num < N) 32 { 33 for(int i = round; i < n - round && num < N; i++) G[round][i] = matrix[num++]; 34 35 for(int i = round + 1; i < m - round && num < N; i++) G[i][n-round-1] = matrix[num++]; 36 37 for(int i = n-round-2; i >= round && num < N; i--) G[m-round-1][i] = matrix[num++]; 38 39 for(int i = m-round-2; i >= round + 1 && num < N; i--) G[i][round] = matrix[num++]; 40 41 round++; 42 } 43 44 for(int i = 0; i < m; i++) 45 { 46 for(int j = 0; j < n; j++) 47 { 48 cout << G[i][j]; 49 if(j != n-1) cout << " "; 50 } 51 if(i != m-1) cout << endl; 52 } 53 return 0; 54 }
"只要你不停止,走慢一点没关系"