結果

問題 No.392 2分木をたどれ
ユーザー くれちーくれちー
提出日時 2016-12-29 10:57:20
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 1,927 ms / 2,000 ms
コード長 751 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 26,452 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 17:51:41
合計ジャッジ時間 4,509 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1,925 ms
4,376 KB
testcase_02 AC 1,927 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <math.h>
#define rep(i, n) for (i = 0; i < n; i++)
#define left 0
#define right 1

int follow(int s, int r) {
	int i, a = s;
	if (r == left) a = 2 * a +  1;
	else if (r == right) a = 2 * a + 2;
	return a;
}

int getbit(int x, int n) {
	return !!(x & (int)pow(2, n - 1));
}

int depth(int n) {
	int i, t = 0;
	rep(i, 12) {
		t += pow(2, i);
		if (t > n) return i;
	}
}

int main() {
	int m;
	scanf("%d", &m);

	int a, i, j, k;
	rep(i, m) {
		scanf("%d", &a);
		int d = depth(a);
		rep(j, pow(2, d)) {
			int t = 0, ans[11];
			rep(k, d) {
				ans[k] = getbit(j, k + 1);
				t = follow(t, ans[k]);
			}
			if (t == a) {
				rep(k, d) putchar(ans[k] == left ? 'L' : 'R');
				printf("\n");
				break;
			}
		}
	}

	return 0;
}
0