您的位置:首页 > 其它

PAT乙级—1050

2018-03-10 22:21 190 查看
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
bool comp(int a, int b){
return a>b;
}
int main() {
int N;
int m=0, n=0;
cin>>N;
for(int i=(int)sqrt(N); i<=N; i++){
if(N%i==0){
m=i;
n=N/m;
break;
}
}
cout<<n<<' '<<m<<endl;
vector<int>v;
int a;
for(int i=0; i<N; i++){
cin>>a;
v.push_back(a);
}
sort(v.begin(), v.end(), comp);
int x=0, y=0, j=0;
vector<vector <int>>b;
//int b[1000][1000]={0};
while(j<N){
while(y+1<n&&!b[x][y+1]) b[x][++y]=v[++j];
while(x+1<m&&!b[x+1][y]) b[++x][y]=v[++j];
while(y-1>=0&&!b[x][y-1]) b[x][--y]=v[++j];
while(x-1>=0&&!b[x-1][y]) b[--x][y]=v[++j];
}
for(int i=0; i<m; i++){
for(int h=0; h<n; h++){
if(h==n-1) cout<<b[i][h]<<endl;
else cout<<b[i][h]<<' ';
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: