1105. Spiral Matrix (25)
来源:互联网 发布:苹果手机文档软件 编辑:程序博客网 时间:2024/05/16 14:14
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 m rows 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<cmath>#include<algorithm>using namespace std;int num[10004];int num2[1000][1000];bool cmp(int a,int b){ return a>b;}int main(){ int t,row,col,i,j,n,flag=0,minn=999999; cin>>t; for(i=1;i<=sqrt(t*1.0);i++) { if(t%i==0) { if(t/i-i<minn) { minn=t/i-i; col=i; } } } row=t/col; for (int i=0;i<t;i++) cin>>num[i]; sort(num,num+t,cmp); int x=0; for (n=0;;n++) { for (j=n;j<col-n;j++) { num2[n][j]=num[x++]; if (t==x) { flag=1; break; } } if (flag) break; for (i=n+1;i<row-n-1;i++) { num2[i][j-1]=num[x++]; if (t==x) { flag=1; break; } } if (flag) break; for (j--;j>=n;j--) { num2[i][j]=num[x++]; if (t==x) { flag=1; break; } } if (flag) break; for (i--;i>n;i--) { num2[i][j+1]=num[x++]; if (t==x) { flag=1; break; } } if (flag) break; } for (i=0;i<row;i++) { for (j=0;j<col;j++) { cout<<num2[i][j]; if (j!=col-1) cout<<" "; } if (i!=row-1) cout<<endl; } return 0;}
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix(25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- 1105. Spiral Matrix (25)
- POJ 3140 Contestants Division 树型DP删边
- spring事务管理
- dhcp 配置服务
- sql自动发邮件功能
- SQL中内连接和外连接
- 1105. Spiral Matrix (25)
- TCP
- Java Exception
- 监控-zabbix
- 几种排序算法的讲解(一)
- 1111. Online Map (30) <Dijkstra>
- Linux下Tomcat启动报错:port already in use
- JDBC(9)—事务(Transaction)
- jdk 源码分析(9)java ReentrantReadWriteLock分析