zcmu--4932: 树查找

4932: 树查找

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 7  Solved: 5
[Submit][Status][Web Board]

Description

有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY。该树是完全二叉树。

Input

输入有多组数据。
每组输入一个n(1<=n<=1000),然后将树中的这n个节点依次输入,再输入一个d代表深度。

Output

输出该树中第d层得所有节点,节点间用空格隔开,最后一个节点后没有空格。

Sample Input

5

1 2 3 4 5

7

7

1 2 3 4 5 6 7

2

0

Sample Output

EMPTY

2 3

HINT

Source

数据结构高分笔记

【分析】下标查找就好  水题

#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		int d;
		scanf("%d",&d);
		if(pow(2,d-1)>a[n-1])
		{
			cout<<"EMPTY\n";
			continue;
		}
		else{
			int x=pow(2,d-1);
			int y=min(n,x+x-1);
			int flag=0;
			for(int i=x-1;i<y;i++)
			{
				if(!flag)cout<<a[i],flag=1;
				else cout<<" "<<a[i];
			}
			cout<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82314187