PAT(初級)1050スパイラル行列

問題の意味

ヘリカル後退行列を埋めるために、配列を考えます。

思考

古典的な蛇ウォークタイトル。コードで直接見て。

コード

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int N;
	cin >> N;
	int n, m;
	for (int i = 1; i * i <= N; ++i) {
		if (N % i == 0) {
			m = i;
			n = N / i;
		}
	}
	vector<int> a(N);
	for (int& e : a) 
		cin >> e;
	sort(a.begin(), a.end(), greater<int>());
	vector<vector<int>> ans(n, vector<int>(m, -1));
	int cnt = 0, x = 0, y = -1;
	while (cnt < N) {
		while (y + 1 < m && ans[x][y + 1] == -1) ans[x][++y] = a[cnt++];
		while (x + 1 < n && ans[x + 1][y] == -1) ans[++x][y] = a[cnt++];
		while (y - 1 >= 0 && ans[x][y - 1] == -1) ans[x][--y] = a[cnt++];
		while (x - 1 >= 0 && ans[x - 1][y] == -1) ans[--x][y] = a[cnt++];
	}
	for (auto it : ans) 
		for (int i = 0; i < it.size(); ++i) 
			cout << it[i] << (i == it.size() - 1 ? '\n' : ' ');
	return 0;
} 

ヒント

問題の時間更新溶液に、より多くの時間、初級ACすべてのコード、参照リンクを

公開された50元の記事 ウォン称賛15 ビュー2644

おすすめ

転載: blog.csdn.net/abcdefbrhdb/article/details/104594645