PAT乙级—1050

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41668995/article/details/79512404
#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_41668995/article/details/79512404