UVa 712:S-Trees

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=653

题目类型: 数据结构, 二叉树

样例输入:

3
x1 x2 x3
00000111
4
000
010
111
110
3
x3 x1 x2
00010011
4
000
010
111
110
0

样例输出:

S-Tree #1:
0011

S-Tree #2:
0011

题目大意:

给一个序列集合VVI {x1, x2, x3, ....,xn}, VVI中不是0就是1  然后有一个n层的树,  每一层同层的都是相同的一个数,这个数取自VVI中, 输入会给出 每一层是VVI中的哪一个

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45527.htm

最后一层是叶子结点, 上面是一串给定的数字。

从跟结点出发, 如果那个结点是0,就往左儿子方向走, 如果是1就往右儿子方向走。 最后落在最后一层的叶子结点上,输出这个数字

解题思路:

这题应该算是这个二叉树专题中很水的一道题了。 不用建树,如果是1就 当前结点*2+1,如果是0就乘。 最后得到一个数字。这个数字再减去前面层的结点之和,然后就可输出对应的结果。

具体看代码。

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n,m;
char termi[200];
char vva[200];
vector<int>vtOrder;  

inline void Solve(){
    char temp[10];
    int val;
    vtOrder.clear();
    for(int i=0; i<n; ++i){
        scanf("%s", temp);
        sscanf(temp+1, "%d", &val);
        vtOrder.push_back(val);
    }
    scanf("%s", termi);
    scanf("%d", &m);
    while(m--){
        scanf("%s", vva+1);
        int pos=1;
        for(int i=0; i<vtOrder.size(); ++i){
            if(vva[vtOrder[i]]=='0')
                pos = pos*2;
            else
                pos = pos*2+1;
        }
        printf("%c", termi[pos-(1<<n)]);
    }
    printf("\n");
}  

int main(){
    freopen("input.txt", "r", stdin);
    int cas=1;
    while(scanf("%d", &n) && n){
        printf("S-Tree #%d:\n", cas++);
        Solve();
        printf("\n");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索数字
, 结点
, 题目
, 输出
, 一个
最后
uva oj、uva和uvb的区别、uvb、uva大学、弗吉尼亚大学,以便于您获取更多的相关知识。

时间: 2024-08-13 20:03:45

UVa 712:S-Trees的相关文章

UVa 10562:Undraw the Trees (不限制儿子个数的树)

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=1503 题目类型: 数据结构, 二叉树 输入与输出: Sample Professor's Trees 2 A | -------- B  C   D   |   | ----- - E   F G # e | ---- f g #     Cor

UVa 10562 Undraw the Trees:二叉树先序遍历

10562 - Undraw the Trees Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=1503 Professor Homer has been reported missing. We suspect that his recent resea

UVa 10303 How Many Trees? (卡特兰数&amp;amp;高精度)

10303 - How Many Trees? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1244 A binary search tree is a binary tree with root k such that any node v in th

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di