首页 > 试题广场 >

宠物猫繁育族谱追溯与遗传病风险评估

[编程题]宠物猫繁育族谱追溯与遗传病风险评估
  • 热度指数:32 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

背景
在正规的宠物繁育体系中,记录宠物的血统和族谱至关重要,这不仅是为了证明其品种的纯正,更重要的是为了排查和预防遗传性疾病。现有一家大型宠物繁育协会,记录了注册宠物猫的基本信息以及它们的繁育后代记录。近期发现一只名为Luna的母猫携带了某种隐性遗传病基因。为了通知相关的饲主进行健康检查,协会需要追溯Luna的所有直系后代(包括子女、孙辈、曾孙辈等)。

表结构和字段说明

  • 表名:cats(宠物猫信息表)
    • cat_id:整数类型,宠物猫的唯一注册编号,主键
    • cat_name:字符串类型,宠物猫的呼名(名字)
    • breed:字符串类型,宠物猫的品种(如缅因猫、布偶猫等)
  • 表名:breeding_records(繁育记录表)
    • parent_cat_id:整数类型,父母猫的注册编号(即繁育者)
    • child_cat_id:整数类型,子代猫的注册编号(即后代)
    • birth_date:日期类型,子代猫的出生日期
    • health_score:浮点数类型,子代猫出生时的健康评分(满分100)

3.问题

请编写一条 MySQL 查询语句,查出由名为'Luna'的宠物猫繁育出的所有直接和间接后代。
查询条件限制:

  1. 仅统计出生日期(birth_date)在 2025年1月1日至2025年12月31日期间的后代(注意:如果中间某一代的出生日期不在2025年,也会导致该分支的追溯中断,实际查询中需确保递归链路上的繁育记录满足日期要求)。
  2. 在结果中,需要计算出每一只后代猫的“综合健康评估指数”(composite_index),计算公式为:基础健康评分(health_score) * (0.95 ^ 世代代数)。其中直接子女的世代代数为1,孙辈为2,以此类推。该指数需四舍五入保留2位小数。

返回字段要求:

  • descendant_id:后代猫的注册编号
  • descendant_name:后代猫的呼名
  • generation:世代代数(子女为1,孙辈为2,依此类推)
  • composite_index:综合健康评估指数(四舍五入保留2位小数)

排序规则
结果须首先按世代代数(generation)进行升序排列;若世代代数相同,则按综合健康评估指数(composite_index)进行降序排列;若综合健康评估指数也相同,则按后代猫的注册编号(descendant_id)进行升序排列。

示例1

输入

-- 创建表结构
DROP TABLE IF EXISTS breeding_records;
DROP TABLE IF EXISTS cats;

CREATE TABLE cats (
    cat_id INT PRIMARY KEY,
    cat_name VARCHAR(100),
    breed VARCHAR(50)
);

CREATE TABLE breeding_records (
    parent_cat_id INT,
    child_cat_id INT,
    birth_date DATE,
    health_score DECIMAL(5, 2)
);

-- 插入示例数据
INSERT INTO cats (cat_id, cat_name, breed) VALUES
(100, 'Luna', 'Ragdoll'),
(101, 'Milo', 'Ragdoll'),
(102, 'Bella', 'Ragdoll'),
(103, 'Leo', 'Ragdoll'),
(104, 'Chloe', 'Ragdoll'),
(105, 'Simba', 'Ragdoll'),
(106, 'Nala', 'Ragdoll');

INSERT INTO breeding_records (parent_cat_id, child_cat_id, birth_date, health_score) VALUES
(100, 101, '2025-02-15', 90.00), -- Luna的子女,世代1
(100, 102, '2025-03-20', 95.00), -- Luna的子女,世代1
(101, 103, '2025-10-10', 88.00), -- Milo的子女 (Luna的孙辈),世代2
(101, 104, '2025-11-05', 88.00), -- Milo的子女 (Luna的孙辈),世代2,健康评分相同
(102, 105, '2025-12-01', 92.00), -- Bella的子女 (Luna的孙辈),世代2
(105, 106, '2024-12-25', 96.00); -- Simba的子女,但出生在2024年,不应计入结果

输出

descendant_id|descendant_name|generation|composite_index
102|Bella|1|90.25
101|Milo|1|85.5
105|Simba|2|83.03
103|Leo|2|79.42
104|Chloe|2|79.42

说明

(注意:Nala (106) 因出生在 2024 年未被统计;Leo (103) 和 Chloe (104) 计算出的综合指数相同,按 ID 升序排列。指数计算示例:Milo 世代1,90 * 0.95^1 = 85.50;Leo 世代2,88 * 0.95^2 = 79.42)
with RECURSIVE temp_cat as (
select c.cat_id,c.cat_name,1 as generation,round(a.health_score*power(0.95,1),2) as composite_index
from breeding_records as a
inner join cats as b on a.parent_cat_id = b.cat_id
inner join cats as c on a.child_cat_id = c.cat_id
where b.cat_name ='Luna' and date_format(a.birth_date,'%Y-%m-%d')=2025

union all

select c.cat_id,c.cat_name,t.generation +1, round(b.health_score*power(0.95,(generation+1)),2) as composite_index
from temp_cat as t
inner join breeding_records as b on t.cat_id = b.parent_cat_id
inner join cats as c on b.child_cat_id = c.cat_id
where date_format(b.birth_date,'%Y-%m-%d')=2025
)
select cat_id as descendant_id,cat_name as descendant_name,generation,composite_index
from temp_cat
order by generation asc,composite_index desc,descendant_id asc
发表于 2026-03-03 16:44:49 回复(0)
with recursive information as(
    select
    cat_id descendant_id,
    cat_name descendant_name,
    health_score,
    parent_cat_id
    from cats c
    inner join breeding_records br on c.cat_id = br.child_cat_id
    where birth_date between '2025-01-01' and '2025-12-31'
),
descendant as(
    select
    descendant_id,
    descendant_name,
    1 as generation,
    health_score
    from information
    where parent_cat_id in (select cat_id from cats where cat_name = 'Luna')

    Union all

    select
    i.descendant_id,
    i.descendant_name,
    generation+1,
    i.health_score
    from information i
    inner join descendant de on de.descendant_id = i.parent_cat_id
)

select
descendant_id,
descendant_name,
generation,
round((health_score)*pow(0.95,generation),2) composite_index
from descendant
order by generation,composite_index desc,descendant_id
发表于 2026-03-01 23:17:50 回复(0)
with RECURSIVE temp1 as(
    select 
        child_cat_id
        ,parent_cat_id
        ,birth_date
        ,1 as depth
    from 
        breeding_records
    where 
        parent_cat_id = 100 
    UNION ALL
    select 
        b.child_cat_id
        ,b.parent_cat_id
        ,b.birth_date
        ,temp1.depth +1 as depth
    from 
        breeding_records b 
    join temp1 on temp1.child_cat_id = b.parent_cat_id
)
select 
    *
from
    temp1
果真吗,孙子生的比爷爷都早
发表于 2026-03-01 17:20:32 回复(0)