1105. Spiral Matrix
2016-04-28 18:21
169 查看
1105. Spiral Matrix (25)
时间限制150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:
12 37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93 42 37 81 53 20 76 58 60 76
#include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<cstring> using namespace std; bool cmp(const int &a,const int &b){ return a>b; } int cal(int x){ int y = sqrt(x); while(y>0){ if(x%y == 0) return y; --y; } return 1; } int main(){ int n,a,flag[105][105],x,y,cnt = 0,num[105][105],sx = 0,sy = 0; vector<int> res; cin>>n; y = cal(n); x = n/y; memset(flag,-1,sizeof(flag)); for(int i = 0;i<x;++i) for(int j = 0;j<y;++j) flag[i][j] = 0; for(int i = 0;i<n;++i){ cin>>a; res.push_back(a); } sort(res.begin(),res.end(),cmp); while(cnt<n){ while(sy<y && flag[sx][sy] != -1){ num[sx][sy] = res[cnt++]; flag[sx][sy] = -1; ++sy; } --sy; ++sx; while(sx<x && flag[sx][sy] != -1){ num[sx][sy] = res[cnt++]; flag[sx][sy] = -1; ++sx; } --sx; --sy; while(sy>=0 && flag[sx][sy] != -1){ num[sx][sy] = res[cnt++]; flag[sx][sy] = -1; --sy; } ++sy; --sx; while(sx>=0 && flag[sx][sy] != -1){ num[sx][sy] = res[cnt++]; flag[sx][sy] = -1; --sx; } ++sx; ++sy; } for(int i = 0;i<x;++i){ cout<<num[i][0]; for(int j = 1;j<y;++j) cout<<" "<<num[i][j]; cout<<endl; } return 0; }
相关文章推荐
- iOS UIScrollView简介及使用技巧
- 自定义PopupWindow轻松实现从底部弹出
- spring boot 自定义logback日志配置文件
- HBase命令(一) -- 库操作
- 怎样更好的使用搜索
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(八)
- python调用bat 整理
- 1104. Sum of Number Segments
- TCP/IP协议族各层的协议汇总
- UVALive6900 Road Repair(树的点分治)
- 人月神话阅读笔记3
- 网络爬虫 -- 批量下载网站图片
- 团队作业五之旅游行业手机APP分析
- OGNL简介
- 15、Java Web 路径问题
- 附录三 嵌入式C程序的编译与调试
- char,wchar_t 长度
- 团队开发——个人工作总结10
- swift - 类的继承
- 哈理工OJ 2116 Maximum continuous product(思维的体操)