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 ;
}

最新文章

  1. PowerShell Start 1 - 使用Get - Help.
  2. HTML5的form表单属性
  3. 2016暑假多校联合---Death Sequence(递推、前向星)
  4. vijos1144(小胖守皇宫)
  5. spring+struts2+mybatis
  6. ACMer
  7. shopnc 商城源码阅读笔记-缓存技术
  8. 数据库下载word预览功能的研究
  9. AngularJs 实例
  10. Android5.1源码Xposed框架编译
  11. 使用 FLASH DATABASE 恢复误删除的用户
  12. Django REST FrameWork中文教程2:请求和响应
  13. easyui提交form表单接受数据处理、
  14. 7;XHTML表单
  15. django--orm关系字段(ForeignKey、OneToOneField、ManyToManyField)详解
  16. Percona XtraDB Cluster集群
  17. C# JArray与JObject 的使用
  18. jquery中的全选、反选、全不选和单删、批删
  19. SQLAlchemy中表结构的一对一
  20. 高通QMI协议

热门文章

  1. 【idea-部署web项目】
  2. jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
  3. 第一章 –– Java基础语法
  4. java基础之介绍
  5. 洛谷P1762 偶数
  6. [Xcode 实际操作]三、视图控制器-(10)在Storyboard中使用图像视图控件
  7. 为什么要把系统拆分成分布式的,为啥要用Dubbo?
  8. Isolation Forest算法实现详解
  9. 洛谷P1057 传球游戏(记忆化搜索)
  10. HttpResponseCache的使用缓存cache