結果

問題 No.392 2分木をたどれ
ユーザー Grun1396Grun1396
提出日時 2016-08-12 17:22:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 56,284 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 02:35:07
合計ジャッジ時間 1,010 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 8 ms
5,376 KB
testcase_02 AC 9 ms
5,376 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