您的位置:首页 > 编程语言 > C语言/C++

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答案正确251105C++
(g++ 4.7.2)
11640datrilla


测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确251213/13
1答案正确45122/2
2答案正确25123/3
3答案正确33762/2
4答案正确23841/1
5答案正确35121/1
6答案正确116403/3
#include<iostream>
#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++