pat 1004

 

Counting Leaves 

 

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

 

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 Input

2 1
01 1 02

Sample Output

0 1

水题一个,主要考察宽搜,树使用链接表的形式进行存储,方便搜索。

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 int A[101][101];
 5 int childNum[101];
 6 int levelNum;
 7 int N, M;
 8 int *currentList, *nextList;
 9 void init()
10 {
11     memset(A, 0, sizeof(int)*101*101);
12     memset(childNum, 0, sizeof(int)*101);
13     int i,j,ID,k;
14     i = 1;
15     while(i<=M){
16         scanf("%d %d", &ID, &k);
17         A[ID][0] = k; //record the child numchildNumer
18         for(j = 1;j<= k;j++)
19             scanf("%d", &A[ID][j]);
20         i++;
21     }
22 }
23 void countLevelChildNum(){
24 
25     int i,j,leafNodeNum,nextNodeNum,temp , *ptemp;
26     levelNum = 1;
27     if(A[1][0] ==0){
28         levelNum = 1;
29         childNum[1] =1;
30         return ;
31     }else{
32         levelNum++;
33         childNum[1] = 0;
34     }
35 
36     //init the current list
37     currentList[0] = A[1][0];
38     i =1;
39     while(i <= A[1][0]){
40         currentList[i] = A[1][i];
41         i++;
42     }
43     while(currentList[0])
44     {
45         leafNodeNum = 0;nextNodeNum =0;
46         for(i =1; i <= currentList[0] ;i++)
47         {    
48             temp = currentList[i];
49             if(A[temp][0] == 0)
50             {
51                 leafNodeNum++;continue; 
52             }
53             for(j =1; j<= A[temp][0];j++)
54             {
55                 nextNodeNum++;
56                 nextList[nextNodeNum] = A[temp][j];
57             }
58 
59         }
60         nextList[0] = nextNodeNum;
61         childNum[levelNum] = leafNodeNum;
62         levelNum ++;
63 
64         ptemp = currentList;
65         currentList = nextList;
66         nextList = currentList;
67     }
68     levelNum--;
69 
70 }
71 int main()
72 {
73     int i, flag = 0;
74 
75     currentList = (int *)malloc(sizeof(int) * 101);
76     if(currentList == NULL) return 0;
77     nextList = (int *)malloc(sizeof(int)*101);
78     if(nextList == NULL) return 0;
79 
80     while(scanf("%d %d",&N,&M) != EOF)
81     { 
82         init();
83         countLevelChildNum();
84     
85         if(flag == 1) 
86             printf("\n");
87         else 
88             flag = 1;
89 
90         printf("%d",childNum[1]);
91         for(i = 2; i <= levelNum; i++) 
92             printf(" %d", childNum[i]);
93     }
94 
95 
96     return 0;
97 }

 

转载于:https://www.cnblogs.com/graph/archive/2013/03/24/2979909.html

### 关于C语言PAT乙级1004题的解法与解析 对于PAT乙级1004题,题目描述涉及处理学生的信息记录。具体来说,程序需要接收一系列的学生数据,每条数据包括学生的姓名、学号以及成绩,并最终按照特定的要求输出这些信息。 #### 题目结构分析 根据给出的内容,在该类问题中,输入的第一行为正整数\(n\)表示后续会有\(n\)组学生信息待录入;而接下来的每一行则对应一位学生具体的个人信息——依次为姓名、学号和成绩[^5]。值得注意的是,这里的姓名和学号长度均不会超过十个字符,而且所有学生的成绩都是独一无二的。 #### 编程实现要点 为了高效完成此类任务,可以考虑如下几个方面: - **存储方式的选择**:由于涉及到对学生信息的操作(如查询最高分最低分的同学),因此推荐使用数组或者链表来保存每位同学的数据。 - **排序逻辑的设计**:当面对求取最值的需求时,可以直接遍历整个列表找到最大最小值对应的索引位置,也可以先对原始数据按一定规则排序后再选取首位元素作为目标对象。 - **格式化输出控制**:最后一步是要严格按照题目规定的格式打印结果,注意各字段间的间隔符及换行等问题。 下面是一个简单的代码框架用于解决这个问题: ```c #include <stdio.h> #include <string.h> typedef struct { char name[11]; char id[11]; int score; } Student; int main() { int n, i; scanf("%d", &n); Student students[n]; for(i = 0; i < n; ++i){ scanf("%s %s %d", students[i].name, students[i].id, &(students[i].score)); } // 找到分数最高的学生 int maxIndex = 0; for(i = 1; i < n; ++i){ if(students[maxIndex].score < students[i].score) maxIndex = i; } // 找到分数最低的学生 int minIndex = 0; for(i = 1; i < n; ++i){ if(students[minIndex].score > students[i].score) minIndex = i; } printf("%s %s\n%s %s\n", students[maxIndex].name, students[maxIndex].id, students[minIndex].name, students[minIndex].id); return 0; } ``` 上述代码实现了基本的功能需求,即读入多名学生的资料并找出其中得分最高者与最低者的相关信息加以展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值