1105. Spiral Matrix (25)
2016-07-05 18:03
253 查看
1105. Spiral Matrix (25)
时间限制150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The
matrix has m rows and n columns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is
the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The
numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12 37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93 42 37 81 53 20 76 58 60 76
输入一个数
num
接着输入一串num个无序的序列
输出序列非升排序后,有m行n列组成的螺旋矩阵,期中m*n=num;而且要求m>=n;并且在num的因子中,m-n最小;
螺旋从水平向右,碰头,垂直向下,碰头,水平向左,碰头,垂直向上,以此类推。
star:→
↑ ↓
←
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
7月05日 17:51 | 答案正确 | 25 | 1105 | C++ (g++ 4.7.2) | 11 | 640 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 2 | 512 | 13/13 |
1 | 答案正确 | 4 | 512 | 2/2 |
2 | 答案正确 | 2 | 512 | 3/3 |
3 | 答案正确 | 3 | 376 | 2/2 |
4 | 答案正确 | 2 | 384 | 1/1 |
5 | 答案正确 | 3 | 512 | 1/1 |
6 | 答案正确 | 11 | 640 | 3/3 |
#include<algorithm>
#include<vector>
using namespace std;
void this_print(vector<int>*order,int *num)
{
cin >> (*num);
for (int i = 0; i < (*num); i++)
{
int tempr;
cin >> tempr;
order->push_back(tempr);
}
}
bool noincreaseCmp(const int &a, const int &b)
{
return (a > b);
}
void get_m_n(int*num, int*m, int*n)
{
(*m) = (*num);
(*n) = 1;
int tn = 2,tm=(*num)/tn;
while (tm >= tn)
{
while ((*num) % tn != 0&&tm>tn)
{
tn++;
tm = (*num) / tn;
}
if (tm >= tn&&(*num) % tn == 0)
{
(*m) = tm;
(*n) = tn;
}
tn++;
tm = (*num) / tn;
}
}
void fill_it(vector<vector<int>>*spiral_m, vector<int>*order, int *m, int *n)
{
int times = 0, x=0,y=0;
for (vector<int>::iterator iter = order->begin(); iter != order->end();times++)
{
for (; y < (*n) - times&&iter != order->end(); y++, iter++)
(*spiral_m)[x][y] = (*iter);
y--; x++;
for (; x < (*m) - times&&iter != order->end(); x++, iter++)
(*spiral_m)[x][y] = (*iter);
x--; y--;
for (; y >=times&&iter != order->end(); y--, iter++)
(*spiral_m)[x][y] = (*iter);
y++; x--;
for (; x >times&&iter != order->end(); x--, iter++)
(*spiral_m)[x][y] = (*iter);
x++; y++;
}
}
void out(vector<vector<int>>*spiral_m, int*m, int*n)
{
for (int i = 0; i < (*m); i++)
{
for (int j = 0; j < (*n)-1; j++)
{
cout << (*spiral_m)[i][j]<<" ";
}
cout << (*spiral_m)[i][(*n)-1]<<endl;
}
}
int main()
{
int m, n, num ;
vector<int>order;
this_print(&order,&num);
sort(order.begin(), order.end(), noincreaseCmp);
get_m_n(&num, &m, &n);
vector<vector<int>>spiral_m(m);
for (int i = 0; i < m; i++)
spiral_m[i].resize(n);
fill_it(&spiral_m, &order, &m, &n);
out(&spiral_m,&m,&n);
system("pause");
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解