結果

問題 No.392 2分木をたどれ
ユーザー Grun1396Grun1396
提出日時 2016-08-12 17:22:40
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 370 ms
使用メモリ 5,156 KB
最終ジャッジ日時 2022-12-13 17:22:46
合計ジャッジ時間 1,134 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>

using namespace std;
/*
ひだりへ行くと2倍+1、右へ行くと2倍+2である。
左シフト+1,左シフト+2
↓逆操作
-1して右シフト、-2して右シフトを繰り返していく?
(奇数のときLを出力して−1して右シフト、
偶数のときRを出力して-2して右シフトする)
奇数なら必ず左に、偶数なら右にある。


0
1 10
11 100 101 110
111 1000 1001 1010 1011 1100 1101 1110
*/
string func(int n){
	string ans = "";
	string ans_2 = "";
	while(n != 0){
		if(n % 2 == 0){//偶数の時
			ans += 'R';
			n -= 2;
			n /= 2;
		}else{//奇数のとき
			ans += 'L';
			n -= 1;
			n /= 2;
		}
	}
	//逆にしてかえす
	for(int d = 0; d < ans.length(); d++){
		//cout << ans[ans.length() - d - 1];
		 ans_2 += ans[ans.length() - d - 1];
	}
	//cout << endl;
	//cout << "ans:" << ans.c_str() << endl;
	return ans_2;
}

int main()
{

	int m;
	int a;
	string data = "";
	cin >> m;
	for(int d = 0; d < m; d++){
		cin >> a;
		//処理する
		data = func(a);
		cout << data.c_str() << endl;
	}

	return 0;
}
0