首页
学习
活动
专区
圈层
工具
发布
30 篇文章
1
PAT (Basic Level) Practice (中文)1054 求平均值 (20 分)
2
PAT (Basic Level) Practice (中文)1051 复数乘法 (15 分)
3
PAT (Basic Level) Practice (中文)1050 螺旋矩阵 (25 分)
4
PAT (Basic Level) Practice (中文)1046 划拳 (15 分)
5
PAT (Basic Level) Practice (中文)1045 快速排序 (25 分)
6
PAT (Basic Level) Practice (中文)1047 编程团体赛 (20 分)
7
PAT (Basic Level) Practice (中文)1043 输出PATest (20 分)
8
PAT (Basic Level) Practice (中文)1042 字符统计 (20 分)
9
PAT (Basic Level) Practice (中文)1040 有几个PAT (25 分)
10
PAT (Basic Level) Practice (中文)1038 统计同成绩学生 (20 分)
11
PAT (Basic Level) Practice (中文)1036 跟奥巴马一起编程 (15 分)
12
PAT (Basic Level) Practice (中文)1034 有理数四则运算 (20 分)
13
PAT (Basic Level) Practice (中文)1032 挖掘机技术哪家强 (20 分)
14
PAT (Basic Level) Practice (中文)1031 查验身份证 (15 分)
15
PAT (Basic Level) Practice (中文)1030 完美数列 (25 分)
16
PAT (Basic Level) Practice (中文)1029 旧键盘 (20 分)
17
PAT (Basic Level) Practice (中文)1028 人口普查 (20 分)
18
PAT (Basic Level) Practice (中文)1027 打印沙漏 (20 分)
19
PAT (Basic Level) Practice (中文)1025 反转链表 (25 分)
20
PAT (Basic Level) Practice (中文)1053 住房空置率 (20 分)
21
PAT (Basic Level) Practice (中文)1024 科学计数法 (20 分)
22
PAT (Basic Level) Practice (中文)1022 D进制的A+B (20 分)
23
PAT (Basic Level) Practice (中文)1049 数列的片段和 (20 分)
24
PAT (Basic Level) Practice (中文)1021 个位数统计 (15 分)
25
PAT (Basic Level) Practice (中文)1048 数字加密 (20 分)
26
PAT (Basic Level) Practice (中文)1019 数字黑洞 (20 分)
27
PAT (Basic Level) Practice (中文)1017 A除以B (20 分)
28
PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
29
PAT (Basic Level) Practice (中文)1015 德才论 (25 分)
30
PAT (Basic Level) Practice (中文)1014 福尔摩斯的约会 (20 分)

PAT (Basic Level) Practice (中文)1050 螺旋矩阵 (25 分)

1050 螺旋矩阵 (25 分)

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

输入格式:

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

输出格式:

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

输入样例:

代码语言:javascript
复制
12
37 76 20 98 76 42 53 95 60 81 58 93

输出样例:

代码语言:javascript
复制
98 95 93
42 37 81
53 20 76
58 60 76

实话说,大一上刚接触c语言的时候做过一题类似的,那时候被整的吐血,现在也差不多hh

首先是找出符合条件的M,N,这个不难只要暴力找一下就好也不会超时,当然要记得先排个序~

大体的思路是顺时针右下左上四个方向,找好起始点和终点与轮数的坐标关系,并用一个cnt变量保存已放入的数字,达到总个数

就结束,这道题其实难就难在起始点与终点的表达,还有最后一轮的特判~

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 300005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
	char ch = getchar(); ll s = 0, w = 1;
	while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
	return s * w;
}
inline void write(ll x)
{
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10 + 48);
}
ll sum=0,a[maxn];
inline bool cmp(ll a,ll b)
{
    return a>b;
}
int main()
{
    cin>>sum;
    ll m,n,minn=inf;
    for(rg i=1;i*i<=sum;i++)
    {
        if(sum%i==0)
        {
            ll j=sum/i;
            if(j-i<minn)
            {
                minn=j-i;
                m=j,n=i;
            }
        }
    }
    //cout<<m<<" "<<n<<endl;
    for(rg i=1;i<=sum;i++)a[i]=read();
    sort(a+1,a+1+sum,cmp);
    ll p[m+10][n+10];
    memset(p,0,sizeof(p));
    ll cnt=0,tepm=m,tepn=n,row=1,c=1;
    while(cnt<=sum&&tepm>0&&tepn>0)
    {
        ll tep=cnt;
        for(rg i=c;i<=c+tepn-1;i++)p[row][i]=a[++cnt];if(cnt>=sum)break;
        for(rg i=row+1;i<=row+tepm-1;i++)p[i][c+tepn-1]=a[++cnt];if(cnt>=sum)break;
        for(rg i=c+tepn-2;i>=c;i--)p[row+tepm-1][i]=a[++cnt];if(cnt>=sum)break;
        for(rg i=row+tepm-2;i>=row+1;i--)p[i][c]=a[++cnt];if(cnt>=sum)break;
        //cout<<tepm<<" "<<tepn<<endl;
        if(tep==cnt)break;
        row++,c++,tepm-=2,tepn-=2;
    }
    for(rg i=1;i<=m;i++)
    {
        for(rg j=1;j<=n;j++)
        {
            j==n?cout<<p[i][j]:cout<<p[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
下一篇
举报
领券