PAT 1004. Counting Leaves C语言
来源:互联网 发布:雅思教材推荐知乎 编辑:程序博客网 时间:2024/06/07 17:46
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
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.
Output
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.
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 Input2 101 1 02Sample Output
0 1
树的建立与遍历。本来想练习一下指针,但这题数据规模不大,还是偷懒用了数组。
#include <stdio.h>int t[100]={0,1},ch[100][100]={0},ans[100]={0},max=0;void find(int f){ int i; if (ch[f][0]==0) { ans[t[f]]++; if (t[f]>max) max=t[f]; return; } for (i=1;i<=ch[f][0];i++){ t[ch[f][i]]=t[f]+1; find(ch[f][i]); }}int main(){ int n,m,i; scanf("%d %d",&n,&m); int f,j; for (i=1;i<=m;i++){ scanf("%d",&f); scanf("%d",&ch[f][0]); for (j=1;j<=ch[f][0];j++){ scanf("%d",&ch[f][j]); } } find(1); for (i=1;i<max;i++) printf("%d ",ans[i]); printf("%d",ans[max]);}
- PAT 1004. Counting Leaves C语言
- PAT Advanced 1004. Counting Leaves (30) (C语言实现)
- 【PAT甲级】【C++】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves
- 【PAT】1004. Counting Leaves
- 1004. Counting Leaves (30)[C语言]
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT-1004. Counting Leaves(Queue)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- HTTP数据请求的讲解
- MatLab建模学习笔记4——MatLab向Excel中读写数据
- SectionIndexer详解
- Oracle手工安装数据库
- centos安装db2个人版失败,没有使用语言包错误
- PAT 1004. Counting Leaves C语言
- Live Template Variables
- 生成p12文件
- HDU 5750 Dertouzos
- 浅谈Android中用到的设计模式
- 学习c++的50条建议
- ffmpeg的tutorial中文版学习笔记(一)
- Groups of Live Templates
- 剑指offer_01_二维数组中的查找