zcmu-4932: 树查找

版权声明:本人大三在读,有错误烦请指正,共同进步- ( ゜- ゜)つロ 乾杯~点赞请按右上角,转载请标明出处: https://blog.csdn.net/hzyhfxt/article/details/83476103

4932: 树查找

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 26  Solved: 16
[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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a) memset(a,0,sizeof(a))
#define cs cout<<"-----"<<endl;
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn = 2e3 + 5;
typedef long long ll;
int a[maxn];
int main()
{
    int n,d;
    while(scanf("%d",&n) && n)
    {
        go(i,1,n) cin>>a[i];
        cin>>d;
        int s = pow(2,d-1);//第d层第一个节点编号
        int c = s;//第d层共有几个节点
        if(s+c-1 > n)
            cout<<"EMPTY"<<endl;
        else
        {
            go(i,s,s+c-1)
            {
                if(i == s)
                    printf("%d",a[i]);
                else
                    printf(" %d",a[i]);
            }
            cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/83476103