携程笔试 数据仓库方向 编程题 三题AC +题目
第一题:
服务平均调用时长 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: 在实际的数据仓库工作中,经常会遇到对日志数据解析的任务。假设有生产服务调用日志的数据,该数据描述着服务所涉及各个计算步骤的耗时和结果返回数。 请通过JAVA或Python语言实现一个方法,计算出服务平均(包括所有step)调用耗时 输入 日志数据,符合条件的日志格式如下: requesttime:2019-06-30 11:00:00, clientip:127.0.0.1, step1:10ms, step2:20ms, step3:10ms, return_cnt:112 ,其中黑体部分由模板生成,固定不变; 由于日志记录的灵活性,有可能会出现一些不符合上述格式的“脏”数据,处理时可以忽略 输出 服务平均调用耗时,精度请保留一位小数(四舍五入) 样例输入 requesttime:2019-06-30 11:00:00, clientip:127.0.0.1, step1:10ms, step2:20ms, step3:10ms, return_cnt:112 requesttime:2019-06-30 11:00:01, clientip:127.0.0.1, step1:30ms, step2:15ms, step3:20ms, return_cnt:11 样例输出 52.5 提示 1. 服务的总耗时等于所有步骤耗时的和。 2.由于日志记录的自由度较大,不满足上述格式的日志可以认为是脏数据,不用参与解析和后续计算;如果所有的数据都是脏数据,结果请输出0.0
代码:
public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); List<String[]> arrlist=new ArrayList<>(); while(scan.hasNext()) { String lines = scan.nextLine(); String[] line=lines.split(","); if(line.length==6){ arrlist.add(line); } } double result=0; for(int i=0;i<arrlist.size();i++){ String[] step1=arrlist.get(i)[2].split(":"); String[] step2=arrlist.get(i)[3].split(":"); String[] step3=arrlist.get(i)[4].split(":"); double num1=Double.parseDouble(step1[1].substring(0,step1[1].length()-2)); double num2=Double.parseDouble(step2[1].substring(0,step2[1].length()-2)); double num3=Double.parseDouble(step3[1].substring(0,step3[1].length()-2)); result=result+num1+num2+num3; } if(arrlist.size()<1){ System.out.println(0.0); }else { System.out.println(String.format("%.1f",result / arrlist.size())); } } }
销售数据Sql题 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: 1. 某超市后台数据库有三张表,分别描述销售情况sales、订单中单商品情况sale_items、商品属性信息product,表结构如下 输入 sales 销售情况表 ( order_id bigint comment '订单id,主键' ,order_time string comment '下单时间,字符串类型,格式如2019-03-01 11:00:00.123' ,total_cnt int comment '购买商品总数(包含订单所有商品)' ,amount bigint comment '购买商品总金额' ) sale_items 销售商品表 ( serial_id bigint comment '流水id,主键' ,order_id bigint comment '订单id' ,product_id bigint comment '商品id' ,cnt int comment '单商品个数' ) product 商品属性表 ( id bigint comment '商品id,主键' ,price bigint comment '商品单价' ,cost bigint comment '商品成本单价' ) 输出 请用SQL查出 2019年6月这一个月中每天最畅销的商品id、销售量、营业额和商品利润(输出字段以orderdate,product_id,totcnt,totprice,totprofit命名) 样例输入 create table sales(order_id string primary key not null, order_time varchar(25),total_cnt bigint, amount bigint); insert into sales values ('A0000000001','2019-06-01 12:00:05',2,200); insert into sales values ('A0000000002','2019-06-06 09:18:05',3,1100); insert into sales values ('A0000000003','2019-06-06 20:10:07',3,3000); insert into sales values ('A0000000004','2019-06-14 12:50:08',3,1200); insert into sales values ('A0000000005','2019-06-14 23:08:09',4,2500); insert into sales values ('A0000000006','2019-06-29 08:00:10',6,3700); create table sale_items(serial_id bigint primary key not null, order_id varchar(25),product_id varchar(2), cnt bigint); insert into sale_items values (1,'A0000000001','A',2); insert into sale_items values (2,'A0000000002','A',1); insert into sale_items values (3,'A0000000002','B',2); insert into sale_items values (4,'A0000000003','C',3); insert into sale_items values (5,'A0000000004','A',2); insert into sale_items values (6,'A0000000004','C',1); insert into sale_items values (7,'A0000000005','B',3); insert into sale_items values (8,'A0000000005','C',1); insert into sale_items values (9,'A0000000006','A',2); insert into sale_items values (10,'A0000000006','B',1); insert into sale_items values (11,'A0000000006','C',3); create table product(id varchar(2) primary key not null, price bigint, cost bigint); insert into product values ('A',100,80); insert into product values ('B',500,350); insert into product values ('C',1000,650); 样例输出 orderdate product_id totcnt totprice totprofit 2019-06-01 A 2 200 40 2019-06-06 C 3 3000 1050 2019-06-14 B 3 1500 450 2019-06-29 C 3 3000 1050代码:
select days as orderdate,product_id,max(tocnt) as totcnt,max(tocnt)*price as totprice,(price-cost)*max(tocnt) as totprofit from( select days,si.product_id as product_id,sum(cnt) as tocnt from sale_items si JOIN ( select order_id,substr(order_time,0,11) as days from sales where substr(order_time,0,8)='2019-06') s on si.order_id=s.order_id group by days,product_id order by days) temp JOIN product p on p.id=temp.product_id group by days
第三题:
拿硬币的问题 时间限制:C/C++语言 1000MS;其他语言 3000MS 内存限制:C/C++语言 65536KB;其他语言 589824KB 题目描述: 假设有 N 个硬币,每次只准从中拿 1个或者 2个,问一共有几种不同的拿法 比如: 2 个硬币,(1,1), (2) 一共 2 种拿法 3 个硬币,(1,1,1), (1,2), (2,1) 一共有 3 种拿法 输入 输入为一个数字,代表 N 个硬币 输出 输出为一个数字,代表拿硬币的方法个数 样例输入 2 样例输出 2代码:
public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); if(n<1){ System.out.println(1); return; } long [] dp=new long[n]; dp[0]=1; dp[1]=2; for(int i=2;i<dp.length;i++){ dp[i]=dp[i-1]+dp[i-2]; } System.out.println(dp[n-1]); } }