• 1050 螺旋矩阵 (25 分


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

    输入格式:

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

    输出格式:

    输出螺旋矩阵。每行 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<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 10010;
    int matrix[maxn][maxn],A[maxn];
    bool cmp(int a,int b){
        return a > b;
    }
    
    int main(){
        int N;
        scanf("%d",&N);
        for(int i = 0; i < N; i++){
            scanf("%d",&A[i]);
        }
        sort(A,A+N,cmp);
        if(N == 1){
            printf("%d",A[0]);
            return 0;
        }
        int m = (int)ceil(sqrt(1.0*N));
        while(N % m != 0) m++;
        int n = N/m,i = 1,j = 1,now = 0; //m is line,n is column
        int U = 1,D = m,L = 1,R = n;
        while(now < N){
            while(now < N && j < R){ //forward to right
                matrix[i][j] = A[now++];
                j++;
            }
            while(now < N && i < D){
                matrix[i][j] = A[now++]; //to down
                i++;
            }
            while(now < N && j > L){  //forward to down
                matrix[i][j] = A[now++];
                j--;
            }
            while(now < N && i > U){        //forward to left
                matrix[i][j] = A[now++];
                i--;
            }
            U++;D--;
            L++;R--;
            i++;j++;
            if(now == N - 1) matrix[i][j] = A[now++];
       }
       for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                printf("%d",matrix[i][j]);
                if(j < n) printf(" ");
                else printf("
    ");
            }
       }
       return 0;
    } 
    //网上的代码,暂留
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<cctype>
    using namespace std;
    int n;
    void solve(){
        int a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int x=sqrt(n),m;
        while(n%x){
            x--;
        }
        m=n/x;
        int t[m][x];
        for(int side=0,k=n-1;side*2<x;side++){
            for(int j=side;j<x-side;j++){
                t[side][j]=a[k--];
            }
            for(int i=side+1;i<m-side;i++){
                t[i][x-1-side]=a[k--];
            }
            for(int j=x-2-side;j>=side;j--){
                t[m-1-side][j]=a[k--];
            }
            if(x-1-side>side){
                for(int i=m-2-side;i>=side+1;i--){
                    t[i][side]=a[k--];
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<x;j++){
                printf("%d",t[i][j]);
                if(j+1<x){
                    printf(" ");
                }
            }
            printf("
    ");
        }
    }
    int main(){
        scanf("%d",&n);
        solve();
        return 0;
    }
  • 相关阅读:
    只要有梦想
    过去这一年
    Importing BizTalk Applications to Production Environment
    SQL Server 2005 – Database Master Key
    Check Page Rank of any web site pages instantly
    An Introduction to SQL Server Service Broker [WORD DOCUMENT]
    Svcutil.exe – Generate the proxy class for the WCF client application
    SQL Server 2005: how to add a linked server
    BizTalk Error: The published message could not be routed
    Enable routing for failed messages in BizTalk 2006
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/10381031.html
Copyright © 2020-2023  润新知