-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1050.cpp
More file actions
62 lines (53 loc) · 1021 Bytes
/
1050.cpp
File metadata and controls
62 lines (53 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int N;
cin>>N;
int *a = new int[N];
//vector<int> a(N);
for(int i=0;i<N;i++)
cin>>a[i];
sort(a,a+N,cmp);
//sort(a.begin(),a.end(),cmp);
int m,n;
for(int i = sqrt((double)N);i>=1;i--){
if(N%i==0){
n=i;
break;
}
}
m = N/n;
vector<vector<int>> b(m,vector<int>(n));//定义二维数组,因为下表是变量,所以这样最方便
int level = m/2+m%2;//有几圈
int t= 0;
for(int i=0;i<level;i++){
for(int j=i;j<=n-i-1&&t<=N-1;j++){//右
b[i][j] = a[t++];
}
for(int j=i+1;j<=m-2-i&&t<=N-1;j++){//下
b[j][n-1-i] = a[t++];
}
for(int j = n-i-1;j>=i&&t<=N-1;j--){//左
b[m-i-1][j] = a[t++];
}
for(int j = m-2-i;j>=i+1&&t<=N-1;j--){//上
b[j][i] = a[t++];
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j!=0)
printf(" ");
printf("%d",b[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}