Result: Accepted
Time: 287ms
Memory: 5260kB
#include<stdio.h>
int b[10100][105];
int main(){
int t,i;
int n,j,k,taxi;
int l,w,s,r;
int a[10100];
int shu;
while(~scanf("%d",&t)){
for(i=1;i<=t;i++){
s=1;
r=1;
shu=1;
scanf("%d",&n);
for(j=1;j<=n;j++){
scanf("%d",&a[j]);
}
for(j=1;j<=n;j++){
for(k=1;k<=n;k++){
if(a[j]>a[k]){
taxi=a[j];
a[j]=a[k];
a[k]=taxi;
}
}
}
for(j=1;j<=n;j++){
if(n%j==0){
if(j<=n/j){
w=j;
l=n/j;
}
}
}
for(j=1;j<=n;j++){
b[s][r]=a[j];
if(r<(w-shu+1)&&s==shu){
r++;
}
else if(r==(w-shu+1)&&s<(l-shu+1)){
s++;
}
else if(r>shu&&s==(l-shu+1)){
r--;
}
else if(s>shu+1&&r==shu){
s--;
}
else if(s==shu+1&&r==shu){
r++;
}
if(s==shu+1&&r==shu+1&&(2*shu)<w){
shu++;
}
}
for(j=1;j<=l;j++){
for(k=1;k<=w;k++){
printf("%d ",b[j][k]);
}
printf("\n");
}
if(i<=t){
printf("\n");
}
}
}
}