UVa712 S-Trees (二叉树)

链接:http://acm.hust.edu.cn/vjudge/problem/19200分析:这题那个xi有啥用?二叉树正好对应二进制,将根结点表示为0,左子树就是i*2+0,右子树为i*2+1,所有叶子结点正好可以用0~(1<<n)-1(n为层数,根结点为第0层)的编号表示。
 1 #include <cstdio>
 2
 3 int main() {
 4     int n, kase = 0;
 5     while (scanf("%d", &n) == 1 && n) {
 6         int x[10]; char buf[200];
 7         for (int i = 0; i < n; i++) scanf("%s", buf);
 8         int m = 1 << n; int leaf[200];
 9         scanf("%s", buf);
10         for (int i = 0; i < m; i++)
11             leaf[i] = buf[i] - ‘0‘;
12         printf("S-Tree #%d:\n", ++kase);
13         int q; scanf("%d", &q);
14         int dir[10];
15         for (int i = 0; i < q; i++) {
16             scanf("%s", buf);
17             int v = 0;
18             for (int j = 0; j < n; j++)
19                 v = v * 2 + buf[j] - ‘0‘;
20             printf("%d", leaf[v]);
21         }
22         printf("\n\n");
23     }
24     return 0;
25 }
时间: 2024-12-11 16:11:02

UVa712 S-Trees (二叉树)的相关文章

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

617.Merge Two Binary Trees 合并两个二叉树

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then

(hdu step 2.3.4)How Many Trees?(大数:求n个节点能够成多少棵二叉树)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 976 Accepted Submission(s): 511   Problem

UVA122 Trees on the level【二叉树】【BFS】

Trees on the level Background Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in

[LeetCode] Merge Two Binary Trees 合并二叉树

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then

UVA 122 -- Trees on the level (二叉树 BFS)

 Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为"()"时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环. 接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是'L'就往左走,走不动时建一颗

[Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors

Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it'

[Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of all possible full binary trees with N nodes.  Each element of the answer is the root node of one possible tree. Each node of each tree in the answer mus

LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap,

UVA 122 Trees on the level 二叉树 广搜

题目链接: https://vjudge.net/problem/UVA-122 题目描述: 给你一种二叉树的构造方法, 让你逐层输出二叉树的节点值, 如果不能够则输出"not complete" 解题思路: 这道题就是硬搞就可以了, 参考紫书去做的, 首先处理输入就是非常麻烦的事情, 用到了sscanf就会轻松很多, 看来C中还是有很多很多的好用的标准库函数可以拿来用的, 例如还有本题中的strchr() , 处理完输入, 然后就去构造数. 然后广搜一遍即可 代码: #include