【PAT】1105. Spiral Matrix (25)
来源:互联网 发布:淘宝 省钱快报 编辑:程序博客网 时间:2024/06/05 14:24
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has mrows and n columns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:1237 76 20 98 76 42 53 95 60 81 58 93Sample Output:
98 95 9342 37 8153 20 7658 60 76
分析: 把矩阵一圈圈的分解,进行赋值,然后输出。
#include <iostream>#include <vector>#include <algorithm>#include <cmath>using namespace std;bool cmp(int a, int b){return a>b;}int main(int argc, char** argv) {int N;scanf("%d",&N);if(N==0){printf("0\n");return 0;} int i,t,j;vector<int> vec;for(i=0; i<N; i++){scanf("%d",&t);vec.push_back(t);}sort(vec.begin(), vec.end(), cmp);int m, n;int minDiff = 9999999;for(i=1; i<=N; i++){for(j=i; j<=N; j++){if(i*j>N){break;}if(i*j==N && (j-i)<minDiff){m=j; n=i;minDiff=(j-i);}}} if(m==N || n==1){for(i=0; i<vec.size(); i++)cout<<vec[i]<<endl;return 0;} vector<vector<int> > matrix;matrix.resize(m+2, vector<int>(n+2,0));int row, col;int rowLow=0, rowHigh=m+1;int colLeft=0, colRight=n+1;int index = 0;while (true){//从左到右 i = rowLow + 1;for (int j = colLeft + 1; j<colRight && i<rowHigh; j++){matrix[i][j] = vec[index++];}if (index == N) break;//从上到下 j = colRight - 1;i++;for (; i<rowHigh && j>colLeft; i++){matrix[i][j] = vec[index++];}if (index == N) break;//从右到左 i = rowHigh - 1;j--;for (; j>colLeft && i>rowLow; j--){matrix[i][j] = vec[index++];}if (index == N) break;//从下到上 i--;j = colLeft + 1;for (; i>rowLow + 1 && j<colRight; i--){matrix[i][j] = vec[index++];}rowLow++; rowHigh--;colLeft++; colRight--;if (rowLow >= rowHigh || colLeft >= colRight){break;}}for(i=1; i<=m; i++){for(j=1; j<=n; j++){if(j==1)printf("%d", matrix[i][j]);elseprintf(" %d", matrix[i][j]);}cout<<endl;}return 0;}
- 【PAT】1105. Spiral Matrix (25)
- PAT 1105. Spiral Matrix (25)
- [pat]1105. Spiral Matrix (25)
- PAT--1105. Spiral Matrix (25)
- PAT 1105. Spiral Matrix (25)
- pat 1105. Spiral Matrix (25)
- 【PAT】1105. Spiral Matrix
- PAT A 1105. Spiral Matrix (25)
- 【PAT】【Advanced Level】1105. Spiral Matrix (25)
- Pat(A) 1105. Spiral Matrix (25)
- PAT 甲级 1105. Spiral Matrix (25)
- PAT 甲级:1105. Spiral Matrix
- pat-a1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)-PAT甲级真题
- [转]PAT甲级练习1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- Go的50度灰:Golang新开发者要注意的陷阱和常见错误
- python scikit learn 文本分类
- HDU 5592 ZYB's Permutation(树状数组)
- HDOJ 5591 ZYB's Game(简单博弈)
- 社会化分享的简单实现
- 【PAT】1105. Spiral Matrix (25)
- Optional变量的?和!的区别
- iOS新闻客户端开发教程-完整教程目录
- Ehcache整合spring配置
- LeetCode 278 : First Bad Version (Java)
- Linux命令-权限设置
- 有向图(2)--深度优先搜索&&广度优先搜索
- SpringMVC深度探险(一) —— SpringMVC前传
- Mangos Zero 编译安装