京东 · 排序 · 算法编程题
京东 排序 时限 1 秒 / 256 MB

题目描述

【背景】:电商平台需要对商家的销售业绩、退款情况和客户满意度进行综合评估,以确定优秀商家和需要改进的商家。
【原始表】:
merchants_underline (商家)表:
·
merchant_id (商家 ID): 商家的唯一标识符,INT
·
merchant_name (商家名称): 商家的名称,VARCHAR(50)
·
industry (行业): 商家所属的行业,VARCHAR(20)
sales_underline (销售)表:
·
sale_id (销售 ID): 销售记录的唯一标识符,INT
·
merchant_id (商家 ID): 关联商家表的商家 ID,INT
·
sale_amount (销售金额): 销售的金额,DECIMAL(10, 2)
refunds_underline (退款)表:
·
refund_id (退款 ID): 退款记录的唯一标识符,INT
·
merchant_id (商家 ID): 关联商家表的商家 ID,INT
·
refund_amount (退款金额): 退款的金额,DECIMAL(10, 2)
satisfaction_underline (满意度)表:
·
satisfaction_id (满意度 ID): 满意度记录的唯一标识符,INT
·
merchant_id (商家 ID): 关联商家表的商家 ID,INT
·
satisfaction_score (满意度得分): 客户对商家的满意度得分,INT(1 - 100 分)
【要求】:根据上述表格,查询出商家的总销售金额、总退款金额、平均满意度得分。查询结果按照商家ID升序排列。要求查询出来的表格的字段如下:
·
merchant_id: 商家 ID。
·
merchant_name :商家名称。
·
total_sales_amount: 总销售金额。
·
total_refund_amount: 总退款金额。
·
average_satisfaction_score: 平均满意度得分。(round保留2位小数)
【示例】
merchants_underline (商家)表:
图片
sales_underline (销售)表:
图片
refunds_underline (退款)表:
图片
satisfaction_underline (满意度)表:
图片
【按要求查出来的表】
图片
【解释】
上述示例中merchant_id是1的商家名称是商家A,总销售额是5000+4000 = 9000;退款总金额是1000,满意度得分是(80+70)/2 = 75

样例共 1 组

样例 1
输入
DROP TABLE IF EXISTS merchants_underline;
DROP TABLE IF EXISTS sales_underline;
DROP TABLE IF EXISTS refunds_underline;
DROP TABLE IF EXISTS satisfaction_underline;
-- 创建表
CREATE TABLE merchants_underline (
    merchant_id INT PRIMARY KEY,
    merchant_name VARCHAR(50),
    industry VARCHAR(20)
);

CREATE TABLE sales_underline (
    sale_id INT PRIMARY KEY,
    merchant_id INT,
    sale_amount DECIMAL(10, 2)
);

CREATE TABLE refunds_underline (
    refund_id INT PRIMARY KEY,
    merchant_id INT,
    refund_amount DECIMAL(10, 2)
);

CREATE TABLE satisfaction_underline (
    satisfaction_id INT PRIMARY KEY,
    merchant_id INT,
    satisfaction_score INT
);

-- 插入数据
INSERT INTO merchants_underline (merchant_id, merchant_name, industry)
VALUES (1, '商家 A', '服装'),
       (2, '商家 B', '电子产品');

INSERT INTO sales_underline (sale_id, merchant_id, sale_amount)
VALUES (1, 1, 5000.00),
       (2, 2, 8000.00),
       (3, 1, 4000.00),
       (4, 2, 6000.00);

INSERT INTO refunds_underline (refund_id, merchant_id, refund_amount)
VALUES (1, 1, 1000.00),
       (2, 2, 1500.00);

INSERT INTO satisfaction_underline (satisfaction_id, merchant_id, satisfaction_score)
VALUES (1, 1, 80),
       (2, 2, 90),
       (3, 1, 70),
       (4, 2, 60);


select * from merchants_underline;
select * from sales_underline;
select * from refunds_underline;
select * from satisfaction_underline;
输出
merchant_id|merchant_name|total_sales_amount|total_refund_amount|average_satisfaction_score
1|商家 A|9000.00|1000.00|75.00
2|商家 B|14000.00|1500.00|75.00

算法解析依据一般

考点:排序

限制 1 秒 / 256MB | 核心代码模式(实现给定函数)

题目画像

  • 源站时限:1 秒(牛客口径,非本题专属门槛)

解题思路

参考方向:排序

先用 O(n log n) 排序把无序变有序,后续处理往往就简单了。

思路框架(排序 通法 · 非本题专属)

  1. 排序后很多性质变简单:相邻关系、前缀性质、二分可行。
  2. 若题目禁止使用排序库函数,则手写快排/归并(归并还能顺带求逆序对)。
  3. 排序常与其他范式组合,比如「排序 + 贪心」「排序 + 二分」「排序 + 双指针」。

实现要点:在 C++ 中用 std::sort,Python 用 sorted();注意自定义比较函数的严格弱序。

复杂度:时间 O(n log n) | 空间 O(log n) ~ O(n)

该范式的通法易错点

  • 自定义比较函数不满足严格弱序会导致运行时崩溃。
  • 排序后丢失原始下标,题目需要下标时记得用 pair 一起排。
本题在源数据中没有官方考点标签,方向由题面特征推断,仅供参考。

样例

样例 1

  • 输入:DROP TABLE IF EXISTS merchants_underline; / DROP TABLE IF EXISTS sales_underline; / DROP TABLE IF EXISTS refunds_underline; / DROP TABLE IF
  • 输出:merchant_id|merchant_name|total_sales_amount|total_refund_amount|average_satisfaction_score / 1|商家 A

解析由校招宝本地引擎整理(依据源站考点标签 / 人工判题标注 / 题面规模信号),非官方题解,仅供思路参考。

本题来源:2024年秋招-京东-测试岗-第2批笔试。

‹ 上一题 全部编程题 下一题 ›
编程算法题为只读内容:无需作答,直接看题与解析 · 本站不提供在线判题 · 解析由校招宝本地引擎整理,非官方题解