1105. Spiral Matrix (25)

102 篇文章 0 订阅
37 篇文章 0 订阅


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值