其他分享
首页 > 其他分享> > PAT 乙级 1050 螺旋矩阵 (25分)

PAT 乙级 1050 螺旋矩阵 (25分)

作者:互联网


#include<iostream>

#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

int main() 
{
	int a[10000];
	int N, m, n;
	cin >> N;
	for (int i = sqrt(N) + 1; i > 0; i--)
		if (N%i == 0) 
		{
			m = i; n = N / i; break;
		}
	if (m < n)  swap(m, n);   
	vector<vector<int>>b(m + 1);   
	for (int i = 0; i <= m; i++)
		b[i].resize(n + 1);
	for (int i = 0; i < N; i++)
		cin >> a[i];
	sort(a, a + N, greater<int>());  
	int i = 1, j = 0, count = 0;
	while (N != count) 
	{
		while (j < n&&b[i][j + 1] == 0) b[i][++j] = a[count++];
		while (i < m&&b[i + 1][j] == 0)b[++i][j] = a[count++];
		while (j > 1 && b[i][j - 1] == 0)b[i][--j] = a[count++];
		while (i > 1 && b[i - 1][j] == 0)b[--i][j] = a[count++];
	}
	for (int i = 1; i <= m; i++) 
	{
		cout << b[i][1];
		for (int j = 2; j <= n; j++)
			cout << " " << b[i][j];
		cout << endl;
	}
	system("pause");
	return 0;
}

标签:1050,25,PAT,int,count,++,while,&&,include
来源: https://blog.csdn.net/qq_43180487/article/details/110243991