菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

这篇具有很好参考价值的文章主要介绍了菜鸟记录:c语言实现PAT甲级1004--Counting Leaves。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

    好消息:与上题的Emergency是同样的方法。坏消息:又错了&&c++真的比c方便太多太多。

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

Input Specification:

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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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

题目分析

    分析家族族谱,算出非叶子节点,就是非孩子数量。,所有的节点用两位数字表示,例如:01,02,06等,其中为了方便,令01为根节点。

    输入: N(100个以内的的节点) M(非叶子节点数)

       【接下的M行内】 ID(非叶子节点)K(所连的叶子 / 孩子节点个数)ID[0] ID[1]...ID[K](K个叶子节点)

    输出:(每层的叶子节点个数)中间用空格隔开————>注意,pat对输出有着固定且严格的格式,所以最后结果的第一个数字前是没有空格的。

    就是用广度优先或深度优先遍历每一层,同时标记每层叶子节点并记录,最后数组输出

个人想法

    其实一开始,我觉得很简单,既然只看叶子节点个数,那每次输入ID[0-K]时,我进行记录不就行了吗,每次输入ID都是不同的层级,最后得到的的不就是每层孩子数,所以我在写输入的时候加了一点点变量,最后输出。

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves菜鸟记录:c语言实现PAT甲级1004--Counting Leaves
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int ID[101];
 6 int book[101];//记录输出每层叶节点个数    
 7 int b;        //每层的叶节点数
 8 
 9 int main() {
10     int N, M;
11     scanf("%d%d", &N, &M);
12     int K;
13     int a = 1;
14     for (int i = 1; i <= N; i+=K+1) {
15         scanf("%d%d", &ID[i], &K);     
16         if( K != 0)
17         for (int j = i+1; j <= i+K; j++) {
18             scanf("%d", &ID[j]);
19             b++;    //没输入一次叶节点数就增加一个
20         }
21         book[a] = b;//记录
22         b = 0;//归零进行下次计算    
23         a++;//记录数组下标移动
24     }
25     printf("%d", book[0]);//输出第一层
26     for (int i = 1; i <= M; i++) {
27         printf(" %d", book[i]);//输出接下来的每层,并于上一个数字隔开
28 
29     }
30     return 0;
31 }            
View Code

    最后得分13分。。。意料之中,举例思考的时候都只考虑了二叉树,编写的过程中便觉的那如果一个节点有有多个非叶节点甚至没有叶节点应该怎么填写。回过头来再看这段,更是觉得搞笑,如果这样可行,那直接记录K就好了。于是尝试着写了第二种,先构建一颗完整的树,然后深度遍历。

    首先,变量的设置

1 int N, M;//同题目N,M
2 int K;
3 typedef struct NODE{
4     int nodes;//记录每个节点拥有的叶子节点数
5     int ID[101];    //记录各个叶子节点
6 };
7 struct NODE child[101];//
8 int book[101];//保存、输出每层叶子节点数
9 int max;    //比较和更新每层的叶子节点数,因为左边的遍历完,右边的还要遍历并记录

    这里使用了结构体而不是简单的数组或者二维数组记录,是因为退出递归的条件需要知道此时节点后续有无别的节点。对比之前的深度遍历我们可以知道,在每次递归的结束条件语句中,会判断后续“无路可走”后才停止并return,1003中是

 if (curlen > mindis[curcity])return;   

  ,即如果所求的路径走这时变长就停止这条路。在此题则是你所谈的节点后面无节点就说明是叶子节点,那么就停止遍历,并记录数量。但是我开始在这使用二维数组,判定条件:

if(child[curnode]==0) return;

认为全局变量初始化为0,而这样判断说明此行均为0时就说明它是空的是叶子节点,但是实际运行出现了死循环,因为你在输入的for语句中都会因为默认给每一行赋值,导致你输入的值令每行都不是全为0(有点混乱)。而c++中用 child.[curnode]size()【child是vector型】判断长度,如果等于0 ,则进入叶子节点的计数。所以对c语言使用结构体记录每个节点的叶子节点数和叶子节点编号。

 1 void dfs(int curnode ,int curlevel);
 2 int main() {
 3     int a;
 4     scanf("%d%d", &N, &M);
 5     for (int i = 0; i < M; i++) {
 6         scanf("%d%d", &a, &K);
 7         child[a].node = K;//非叶节点ID,所有的叶子节点数
 8         for (int j = 0; j <K; j++) {
 9             scanf("%d", &child[a].ID[j]);  //叶子节点序号
10         }
11     }
12     dfs(1, 0);//从 1 开始是因为01为根节点,所有存放元素的是child[01]不是child[0]
13     printf("%d", book[0]);
14     for (int i = 1; i <= max; i++) {
15         printf(" %d", book[i]);
16 
17     }
18     return 0;
19 }
20 void dfs(int curnode, int curlevel) {
21     if (child[curnode].node == 0) {   //判断有无叶节点,无则进入此记录保存
22         if(max<curlevel)
23         max = curlevel;        //更新当前遍历所在的叶子节点层数
24         book[curlevel]++;        //没到该层,叶子节点数加 1 
25         return;
26     }
27     for (int i = 0; i < child[curnode].node; i++) {    //因为对该节点进行遍历,所以在它有的叶子节点数范围内循环
28         dfs(child[curnode].ID[i], curlevel + 1);    //把当前的某个叶子节点的数值带入下一个节点的索引,层级加1.
29     }
30  }

 


<-----------------------------------完整代码----------------------------------->

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves菜鸟记录:c语言实现PAT甲级1004--Counting Leaves
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 
 7 int N, M;
 8 int K;
 9 typedef struct NODE{
10     int node;
11     int ID[101];
12 };
13 struct NODE child[101];
14 int book[101];
15 int max;
16 
17 void dfs(int curnode ,int curlevel);
18 int main() {
19     int a;
20     scanf("%d%d", &N, &M);
21     for (int i = 0; i < M; i++) {
22         scanf("%d%d", &a, &K);
23         child[a].node = K;
24         for (int j = 0; j <K; j++) {
25             scanf("%d", &child[a].ID[j]);
26         }
27     }
28     dfs(1, 0);
29     printf("%d", book[0]);
30     for (int i = 1; i <= max; i++) {
31         printf(" %d", book[i]);
32 
33     }
34     return 0;
35 }
36 void dfs(int curnode, int curlevel) {
37     if (child[curnode].node == 0) {
38         if(max<curlevel)
39         max = curlevel;
40         book[curlevel]++;
41         return;
42     }
43     for (int i = 0; i < child[curnode].node; i++) {
44         dfs(child[curnode].ID[i], curlevel + 1);
45     }
46  }
View Code

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

 

总结

    其实掌握深度遍历总体大概的流程,结合题目改变判定条件写出总体的框架基本都能得分,然后调试以后进行修改就大差不差了。

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

菜鸟记录:c语言实现PAT甲级1004--Counting Leaves文章来源地址https://www.toymoban.com/news/detail-416431.html

到了这里,关于菜鸟记录:c语言实现PAT甲级1004--Counting Leaves的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 菜鸟记录PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(25)
  • PAT 甲级【1010 Radix】

    本题范围long型(35)^10 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举 代码如下 本页面将简要介绍二分查找,由二分法衍生的三分法以及二分答案。 二分查找(英语:binary search),也称折半搜索(英语:half-interval search)、对数搜索(英语:logar

    2024年02月08日
    浏览(27)
  • PAT 甲级考试【1003 Emergency】

    题目: As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead

    2024年02月08日
    浏览(30)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(29)
  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(22)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(31)
  • 1083 List Grades (PAT甲级)

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. Input Specification: Each input file contains one test case. Each case is given in the following format: where  name[i]  and  ID[i

    2024年02月08日
    浏览(41)
  • 1072 Gas Station (PAT甲级)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    浏览(28)
  • PAT甲级图论相关题目

    PAT甲级图论相关题目: 分数 25 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some o

    2024年01月21日
    浏览(40)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包