結果

問題 No.401 数字の渦巻き
ユーザー LogicR_piman
提出日時 2016-09-26 04:08:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 62,440 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-18 15:04:36
合計ジャッジ時間 1,385 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>

using namespace std;

int main() {
	int n, nn, i, j, k, x = 0, y = 0, d = 0, e = 0, f = 0;
	string s, ii;

	cin >> n;
	nn = n*n;

	for (i = 0; i < n-1; i++) {
		for (j = 0; j < n * 4; j++) {
			s.insert(0, "0");
		}
		s.insert(0, "\n");
	}
	for (j = 0; j < n * 4; j++) {
		s.insert(0, "0");
	}
	//0と改行で構成された文字列S

	for (i = 1; i <= n; i++) {
		if (i == 1) x = 0;
		else x += 1;
		if (i < 10) d = 2;
		else if (i < 100) d = 3;
		else d = 4;
		e = y * (n * 4 + 1) + x * 4 - d + 4;
		ii = to_string(i) + " ";
		s.replace(e, d, ii);
	}
	//1行目は例外
	while(1){
		if (i > n) {
			for (k = 0; k < n - 1 - f; k++) {
				y += 1;
				if (i < 10) d = 2;
				else if (i < 100) d = 3;
				else d = 4;
				e = y * (n * 4 + 1) + x * 4 - d + 4;
				ii = to_string(i) + " ";
				s.replace(e, d, ii);
				i++;
			}
			for (k = 0; k < n - 1 - f; k++) {
				x -= 1;
				if (i < 10) d = 2;
				else if (i < 100) d = 3;
				else d = 4;
				e = y * (n * 4 + 1) + x * 4 - d + 4;
				ii = to_string(i) + " ";
				s.replace(e, d, ii);
				i++;
			}
			if (i > nn) break;
			for (k = 0; k < n - 2 - f; k++) {
				y -= 1;
				if (i < 10) d = 2;
				else if (i < 100) d = 3;
				else d = 4;
				e = y * (n * 4 + 1) + x * 4 - d + 4;
				ii = to_string(i) + " ";
				s.replace(e, d, ii);
				i++;
			}
			for (k = 0; k < n - 2 - f; k++) {
				x += 1;
				if (i < 10) d = 2;
				else if (i < 100) d = 3;
				else d = 4;
				e = y * (n * 4 + 1) + x * 4 - d + 4;
				ii = to_string(i) + " ";
				s.replace(e, d, ii);
				i++;
			}
			if (i > nn) break;
			f += 2;
		}
	}
	cout << s << endl;
	return 0;
}
0