标签:matrix while sys out style name std space ios
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> #include<queue> #include<set> #include<stack> using namespace std; int main() { int num; cin >> num; int arr[100001]; for (int i = 0; i < num; i++) { cin >> arr[i]; } sort(arr, arr + num); reverse(arr, arr + num); int matrix[100][100]; fill(matrix[0], matrix[0]+10000, 0); int m = sqrt(num); int n; for (m; num%m != 0; m--) { } n = num / m; int a, b,i; i = a = b = 0; int index = 1; while (i < num) { while (b < m && index == 1 && matrix[a][b]==0) { matrix[a][b] = arr[i]; b++; i++; if (b == m || matrix[a][b] != 0) { index = 2; a++; b--; } } while (a < n && index == 2 && matrix[a][b] == 0) { matrix[a][b] = arr[i]; a++; i++; if (a==n || matrix[a][b] != 0) { index = 3; b--; a--; } } while (b>=0 && index == 3 && matrix[a][b] == 0) { matrix[a][b] = arr[i]; b--; i++; if (b==-1|| matrix[a][b] != 0) { index = 4; a--; b++; } } while (a >= 0 && index == 4 && matrix[a][b] == 0) { matrix[a][b] = arr[i]; a--; i++; if (a == -1 || matrix[a][b] != 0) { index = 1; b++; a++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j != m - 1) { cout << matrix[i][j]<<‘ ‘; } else { cout << matrix[i][j]; } } cout << endl; } system("pause"); }
填矩阵。。。还是挺简单的,就是上在边界的情况处理要小心一点,经常搞错,要往后退一位,再向新的方向进一位才能到正确的位置。。。
标签:matrix while sys out style name std space ios
原文地址:http://www.cnblogs.com/wsggb123/p/7261329.html