1、用vector建立二维数组:

 vector<vector<int>> arr(rows);
for (int i = ; i < rows; i++)
arr[i].resize(cols);

或:

 vector<vector<int>> v(m, vector<int>(n));

2、当数组已初始化但某些下标还未赋值时,此下标对应的变量为NULL(见34行)

代码:

 #include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std; bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = ; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end(), cmp);
int s = sqrt(n);
while (n % s) s--;
int cols = s, rows = n / s;
/*
vector<vector<int>> arr(rows);
for (int i = 0; i < rows; i++)
arr[i].resize(cols);
*/
vector<vector<int>> arr(rows, vector<int>(cols));
int i = , j = , t = ;
int direction = ;
while (t < n)
{
if (direction == )
{
if (j < cols && arr[i][j] == NULL)
arr[i][j++] = v[t++];
else
{
i++;
j--;
direction = ;
}
}
else if (direction == )
{
if (i < rows && arr[i][j] == NULL)
arr[i++][j] = v[t++];
else
{
j--;
i--;
direction = ;
}
}
else if (direction == )
{
if (j >= && arr[i][j] == NULL)
arr[i][j--] = v[t++];
else
{
i--;
j++;
direction = ;
}
}
else if (direction == )
{
if (i >= && arr[i][j] == NULL)
arr[i--][j] = v[t++];
else
{
i++;
j++;
direction = ;
}
}
}
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
if (j) cout << " ";
cout << arr[i][j];
}
cout << endl;
}
return ;
}

pat乙级1050螺旋矩阵的更多相关文章

  1. PAT(B) 1050 螺旋矩阵(Java:24分)

    题目链接:1050 螺旋矩阵 (25 point(s)) 题目描述 本题要求将给定的 N 个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上 ...

  2. PAT Basic 1050 螺旋矩阵 (25 分)

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

  3. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

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

  4. PAT 1050 螺旋矩阵(25)(代码)

    1050 螺旋矩阵(25)(25 分) 本题要求将给定的N个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方 ...

  5. PAT-乙级-1050. 螺旋矩阵(25)

    1050. 螺旋矩阵(25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求将给定的N个正整数按非递增的 ...

  6. 1050. 螺旋矩阵(25) pat乙级题

    本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条件:m*n等于N:m>=n:且m- ...

  7. PAT 1050. 螺旋矩阵(25)

    本题要求将给定的N个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条 ...

  8. PAT——1050. 螺旋矩阵

    本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条件:m*n等于N:m>=n:且m- ...

  9. PAT 1050 螺旋矩阵

    https://pintia.cn/problem-sets/994805260223102976/problems/994805275146436608 本题要求将给定的 N 个正整数按非递增的顺序 ...

随机推荐

  1. 【转-mysql索引失效的几种情形】

    索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件 ...

  2. C# Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

    一.问题描述 在做C# 的 Guid 转换时,出现这个问题:Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-x ...

  3. 交叉编译Spice-gtk

    Fedora环境 编译环境 操作系统: 64位 Fedora23 下载源文件 spice-gtk.spice-protocol 安装依赖 $ sudo yum install -y dh-autore ...

  4. laravel5.4 正式环境 linux 下出现undefined code

    1.laravel5.4 正式环境  linux 下出现undefined code问题 报错位置在:登录模块中login 引用  报$_SESSION['code'] 中的code 并未定义 原因: ...

  5. 2010辽宁省赛E(Bellman_Ford最短路,状态压缩DP【三进制】)

    #include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;struct node{    int v,z,d, ...

  6. PAT1089【归并排序】

    这题略...恶心.. 他说归并排序依次是相邻有序两块合并,而一向打惯了递归??? #include <bits/stdc++.h> using namespace std; typedef ...

  7. 用户与授权:MySQL系列之六

    一.用户管理 1.用户账号 用户的账号由用户名和HOST俩部分组成('USERNAME'@'HOST') HOST的表示: 主机名 具体IP地址 网段/掩码 可以使用通配符表示,%和_:192.168 ...

  8. kubectl 命令

    Kubectl 命令表 kubectl run kubectl expose kubectl annotate kubectl autoscale kubectl convert kubectl cr ...

  9. Android Studio模拟器的root权限

    前言 一个安卓练习中用自带的sqlite3数据库查看数据的时候,需要通过adb shell进入/data/data/[包名]/databases/目录中,通过查看创建的数据库来查看相应的数据或者表.起 ...

  10. 2017年江西理工大学C语言程序设计竞赛(高级组)

    问题 A: 求近似值 #include <stdio.h> #include <time.h> #include <stdlib.h> using namespac ...