結果

問題 No.566 だいたい完全二分木
ユーザー fjafjafja
提出日時 2017-09-10 00:21:19
言語 Java
(openjdk 23)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 730 bytes
コンパイル時間 3,616 ms
コンパイル使用メモリ 79,644 KB
実行使用メモリ 42,552 KB
最終ジャッジ日時 2024-11-07 10:21:49
合計ジャッジ時間 6,221 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11
権限があれば一括ダウンロードができます

ソースコード

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