pat乙级1050螺旋矩阵
2024-09-24 19:04:15
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 ;
}
最新文章
- PowerShell Start 1 - 使用Get - Help.
- HTML5的form表单属性
- 2016暑假多校联合---Death Sequence(递推、前向星)
- vijos1144(小胖守皇宫)
- spring+struts2+mybatis
- ACMer
- shopnc 商城源码阅读笔记-缓存技术
- 数据库下载word预览功能的研究
- AngularJs 实例
- Android5.1源码Xposed框架编译
- 使用 FLASH DATABASE 恢复误删除的用户
- Django REST FrameWork中文教程2:请求和响应
- easyui提交form表单接受数据处理、
- 7;XHTML表单
- django--orm关系字段(ForeignKey、OneToOneField、ManyToManyField)详解
- Percona XtraDB Cluster集群
- C# JArray与JObject 的使用
- jquery中的全选、反选、全不选和单删、批删
- SQLAlchemy中表结构的一对一
- 高通QMI协议
热门文章
- 【idea-部署web项目】
- jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
- 第一章 –– Java基础语法
- java基础之介绍
- 洛谷P1762 偶数
- [Xcode 实际操作]三、视图控制器-(10)在Storyboard中使用图像视图控件
- 为什么要把系统拆分成分布式的,为啥要用Dubbo?
- Isolation Forest算法实现详解
- 洛谷P1057 传球游戏(记忆化搜索)
- HttpResponseCache的使用缓存cache