IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> PTA 1004 Counting Leaves (30 分)(详细翻译+注释) -> 正文阅读

[开发测试]PTA 1004 Counting Leaves (30 分)(详细翻译+注释)

背景:

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

族谱通常由系谱树来表示。你的工作是统计那些没有孩子的家庭成员总数。

输入格式:

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.

每个输入文件包含一个测试用例。每一种情况都以包含以下内容:第一行有一个正整数N(0<N<100),它代表节点的总数,还有一个正整数M,代表不是叶节点的节点总数;然后接着有M行,每一行的格式都是 ID k ID[1] ID[2]…ID[K],其中第一个ID是一个两位数,代表一个给定的非叶节点,K是它的子节点的数量,后面是它的全部子节点的两位数ID序列。为了简单起见,规定根节点的ID为01。

输出格式:

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.

对于每个测试用例,你应该从根节点开始,计算每一层(同辈)的没有孩子的家庭成员总数。结果必须在一行中输出,用空格隔开,并且在每行的末尾不能有额外的空格。示例示例表示一棵只有2个节点的树,其中01是根节点,02是唯一的子节点。因此在根01层上,有0个叶节点;下一层,有1个叶节点。所以应该在一行中输出0 1。

输入样例:

2 1
01 1 02

输出样例:

0 1

思路:

使用深度优先遍历算法,但要注意,必须额外设置一个变量来弄清节点是否是叶节点。

C实现代码:

#include <stdio.h>

int N,M,a,b,map[101][101],book[101],num[101];
//节点数、非叶节点数、父节点、子节点、存图数组、标记数组、统计数组
const int max = 99999999;//最大值

void dfs(int cur,int dep)//当前节点符号,当前深度
{
    int x = 0;//用来判别该节点是否是叶节点
    for(int i = 1;i <= N;i++)
        if(book[i] == 0 && map[cur][i] != max)//不是叶节点
        {
            book[i] = 1;
            x = 1;
            dfs(i,dep + 1);
            book[i] = 0;
        }
    if(x == 0)//是叶节点
        num[dep] += 1;
    return;
}

int main()
{
    int c;//工具数
    for(int i = 1;i <= 100;i++)//不可走的路径设值为max
        for(int j = 1;j <= 100;j++)
            if(i != j)
                map[i][j] = max;
    scanf("%d%d",&N,&M);
    for(int i = 1;i <= M;i++)//可走的路径设值为1
    {
        scanf("%d%d",&a,&c);
        for(int j = 1;j <= c;j++)
        {
            scanf("%d",&b);
            map[a][b] = 1;
            map[b][a] = 1;
        }
    }
    book[1] = 1;//从1号开始走所以将其标记
    dfs(1,1);//深度优先函数
    for(int i = 1;i <= 100;i++)//求出最大深度
        if(num[i] != 0)
            c = i;
    for(int i = 1;i <= c;i++)//输出结果
        if(i != c)
            printf("%d ",num[i]);
        else if(i == c)
            printf("%d",num[i]);
    return 0;
}
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 16:58:56  更:2021-08-12 17:00:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/18 16:29:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码