#include<stdio.h>
void SetArray(int arr[10][10], int m, int n)
{
int i=1,j=1,U=1,D=m,L=1,R=n,now=0,num=10;
int N=m*n;
while(now < N)
{
while(now<N && j<R)
{
arr[i][j++]=num++;
now++;
}
while(now<N && i<D)
{
arr[i++][j]=num++;
now++;
}
while(now<N && j>L)
{
arr[i][j--]=num++;
now++;
}
while(now<N && i>U)
{
arr[i--][j]=num++;
now++;
}
i++,j++;
U++,D--,R--,L++;
if(now == N-1)
{
arr[i][j]=num++;
now++;
}
}
}
int main(){
int arr[10][10];
SetArray(arr,8,7);
for(int i=1;i<=8;i++){
for(int j=1;j<=7;j++){
printf(j==1?"%d":" %d",arr[i][j]);
}
printf("\n");
}
return 0;
}
PATB1050
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=10005;
int arr[maxn];
int matrix[maxn][1000]={0};
int N;
bool cmp(int a, int b){
return a>b;
}
int main(){
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%d",&arr[i]);
}
if(N==1)
{
printf("%d",arr[0]);
return 0;
}
sort(arr,arr+N,cmp);
int m=(int)ceil(sqrt(1.0*N));
while(N%m!=0){
m++;
}
int n=N/m;
int i=1,j=1,U=1,D=m,L=1,R=n,now=0;
while(now < N)
{
while(now<N && j<R)
{
matrix[i][j]=arr[now++];
j++;
}
while(now<N && i<D)
{
matrix[i][j]=arr[now++];
i++;
}
while(now<N && j>L)
{
matrix[i][j]=arr[now++];
j--;
}
while(now<N && i>U)
{
matrix[i][j]=arr[now++];
i--;
}
i++,j++;
U++,D--,R--,L++;
if(now == N-1)
{
matrix[i][j]=arr[now++];
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
printf(j==1?"%d":" %d",matrix[i][j]);
}
printf("\n");
}
return 0;
}