• 1050 螺旋矩阵 (25分)


    本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n列,满足条件:m×n 等于 N;mn;且 mn 取所有可能值中的最小值。

    输入格式:

    输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 10​^4​​,相邻数字以空格分隔。

    输出格式:

    输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。

    输入样例:

    12
    37 76 20 98 76 42 53 95 60 81 58 93
    
     

    输出样例:

    98 95 93
    42 37 81
    53 20 76
    58 60 76

    这个题目我觉得一点都不简单,就是模拟就好了
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    int main(){
        int N;
        cin>>N;
        int m,n;
        for(int i=1;i*i<=N;i++){
            if(N%i==0)n=i;
        }
        m=N/n;
        vector<vector<int> > r(m,vector<int>(n,0));
        vector<int> a(N,0);
        for(int i=0;i<N;i++){
            cin>>a[i];
        }
        sort(a.begin(),a.end(),[](int& x,int& y){
            return x>y;
        });
        int type=0;
        int x=0,y=0,k=0;
        int up=0,down=m,left=0,right=n;
        while(up<down&&left<right){
            if(x<up||x>=down||y<left||y>=right){
                if(type==0){
                    up++;
                    y--;
                    x++;
                }
                else if(type==1){
                    right--;
                    x--;
                    y--;
                }
                else if(type==2){
                    down--;
                    y++;
                    x--;
                }
                else{
                    left++;
                    x++;
                    y++;
                }
                if(up==down||left==right)break;
                type=(type+1)%4;
            }
            //cout<<"x="<<x<<" y="<<y<<" "<<a[k]<<endl;
            r[x][y]=a[k++];
            if(type==0){
                y++;
            }
            else if(type==1){
                x++;
            }
            else if(type==2){
                y--;
            }
            else{
                x--;
            }
        }
        for(int i=0;i<m;i++){
            cout<<r[i][0];
            for(int j=1;j<n;j++){
                cout<<" "<<r[i][j];
            }
            cout<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    element的导航栏修改其选中时颜色的问题
    如何设置element的时间选择器,当日后面的时间无法选中
    Mybatis3配置详解
    mybatis-generator mybatis逆向生成
    Oracle中Thin和OCI的区别
    Jetbrains系列之Phpstorm使用sftp远程开发线上项目
    Thinkphp6.0文档部分摘抄
    linux + 宝塔 + thinkphp5.0 搭建后端api各种问题集合
    Oracle用户,角色和权限
    CentOS7.6 安装 Oracle19c
  • 原文地址:https://www.cnblogs.com/Dancing-Fairy/p/12666077.html
Copyright © 2020-2023  润新知