点击链接PAT甲级-AC全解汇总
题目:
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input Specification: ID K ID[1] ID[2] … ID[K] where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01. The input ends with N being 0. That case must NOT be processed. Output Specification: The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line. Sample Input: Sample Output: 题意: 我的代码: 注意: 输入不一定是根节点开头,一开始做错了,仔细读题发现固定root的id为01,如果输入第一个父节点不是01的话,就没办法直接计算层号了,所以得先把所有数据输入之后在进行统计 如果输入是按照层输入的话,可以用下面的代码:
Each input file contains one test case. Each case starts with a line containing 0
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.2 1
01 1 02
0 1
第一行输入节点数(貌似没啥用) 和所有父节点数,也就是之后输入的行数
然后每行输入当前节点编号 他的子节点数 和一系列子节点
输出要求是输出每层有多少个叶节点#include
#include
评论记录:
回复评论: