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 nsatisfy 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 1. 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<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; //数字不能太大
int matrix[maxn][maxn],A[maxn]; bool cmp(int a,int b){
return a > b;
} int main(){
int N;
scanf("%d",&N);
for(int i = ; i < N; i++){
scanf("%d",&A[i]);
}
if(N == ){
printf("%d",A[]);
return ;
}
sort(A,A+N,cmp);
int m = (int)ceil(sqrt(1.0*N));
while(N % m != ) m++; //除不整的时候m++
int n = N / m, i = , j = , now = ;
int U = , D = m, L = , R = n;
while(now < N){
while(now < N && j < R){
matrix[i][j] = A[now++];
j++;
}
while(now < N && i < D){
matrix[i][j] = A[now++];
i++;
}
while(now < N && j > L){
matrix[i][j] = A[now++];
j--;
}
while(now < N && i > U){
matrix[i][j] = A[now++];
i--;
}
U++,D--,L++,R--;
i++,j++;
if(now == N - ){
matrix[i][j] = A[now++];
}
}
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
printf("%d",matrix[i][j]);
if(j < n) printf(" "); //j < n,不是j < n - 1
else printf("\n");
}
}
return ;
}

最新文章

  1. Eclipse启动Tomcat时server.xml和content.xml自动还原问题
  2. 【Beta】Scrum03
  3. easyUI跨Tab操作datagrid
  4. 编程模式之观察者模式(Observer)
  5. HDOJ(1238) KMP
  6. python---hashlib
  7. quick cocos 的scheduler 定时器
  8. thinkphp文章列表及删除文章
  9. strstr strchr strrchr
  10. POJ 2411 Mondriaan&#39;sDream(状压DP)
  11. POJ 1743 后缀数组不重叠最长重复子串
  12. java隐士类型转换和强制类型转换
  13. 转载SSIS中的容器和数据流—数据转换(Transformations)
  14. 文成小盆友python-num9 socket编程
  15. 性能优化之Hibernate缓存讲解、应用和调优
  16. 08-C语言循环
  17. docker 设计原理
  18. MySQL慢查询日志配置方式 slow_query_log
  19. codeforces467C
  20. Codeforces 798D Mike and distribution

热门文章

  1. windows 2013 datacenter 安装sql server2008 r2兼容性
  2. [CERC 2008] Suffix reconstruction
  3. 汇编题目:在屏幕中间显示a-z的所有字母,按ESC键改变字符颜色
  4. 发布倒计时!JDK11为我们带来哪些新特性?
  5. mount总结
  6. 关于Confusion Matrix
  7. 关于Windows文件读写_暗涌_新浪博客
  8. DES加密/解密
  9. java 设置Cookie值
  10. MySQL在linux下安装