結果

問題 No.566 だいたい完全二分木
ユーザー fjafjafjafjafjafja
提出日時 2017-09-10 00:21:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 730 bytes
コンパイル時間 4,181 ms
コンパイル使用メモリ 78,996 KB
実行使用メモリ 42,108 KB
最終ジャッジ日時 2024-04-25 00:56:05
合計ジャッジ時間 5,492 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
41,528 KB
testcase_01 AC 129 ms
41,372 KB
testcase_02 AC 129 ms
41,632 KB
testcase_03 AC 129 ms
41,212 KB
testcase_04 AC 130 ms
41,580 KB
testcase_05 AC 134 ms
41,240 KB
testcase_06 AC 140 ms
41,740 KB
testcase_07 AC 149 ms
41,468 KB
testcase_08 AC 172 ms
41,704 KB
testcase_09 AC 186 ms
40,944 KB
testcase_10 AC 194 ms
42,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

public class N566 {
	static int[] h;
	static int K;

	public static void main(String[] args)
	{
		Scanner sc=new Scanner (System.in);
		K=sc.nextInt();
		int p=(int)Math.pow(2, K);

		h=new int[p-1];
		h[0]=p/2;
		tree(p/2,0,1);
		swap(h,0,p-2);
		for(int ans:h)
		{
			System.out.print(ans+" ");
		}

	}


	static void tree(int p,int k,int rank)
	{
		h[k]=p;
		//if((int)Math.pow(2, K-1)-1<=k||k<=(int)Math.pow(2,K)-2)
		if(rank==K)
		{
			return;
		}
		else
		{
			rank++;
			tree(p-(int)Math.pow(2, K-rank),2*k+1,rank);
			tree(p+(int)Math.pow(2,K-rank),2*k+2,rank);
		}
	}

	static void swap(int[] h,int n1,int n2)
	{
		int buf=h[n2];
		h[n2]=h[n1];
		h[n1]=buf;
		return ;
	}

}
0