拼多多笔试题在哪能看到,想把第三题写完,忘记一些条件了

如题,牛客用了这么久了还是不熟悉找这找那的规则,各种板块#拼多多#
全部评论
[编程|25分] 简单易懂的秒杀服务 时间限制:5秒 空间限制:65536K 题目描述 实现一个简易的秒杀服务,有3个接口:  1. 添加一个秒杀活动 addActivity(int startTime, int endTime, int goodsId, int limitQuantity)      参数说明:         * 时间区间为左闭右开:[startTime, endTime) 。数据保证 startTime < endTime,startTime 大于当前时间戳         * goodsId 一定为存在的商品id。每个goodsId至多添加一次秒杀活动。         * limitQuantity > 0      返回值有以下两种情况:         * 添加成功,返回秒杀活动ID (从0开始自增)         * 秒杀商品数量大于商品库存,添加失败,返回-1  2. 购买秒杀商品 buyGoods(int activityId, int quantity)      参数说明:         * activityId 一定是请求时存在的活动id         * quantity > 0      返回值有以下三种情况:         * 购买成功,减少库存,返回0         * 购买数量大于秒杀商品剩余数量,购买失败,返回-1         * 秒杀未开始或已结束,购买失败,返回-1  3. 获取秒杀活动列表 getActivityList() 获取当前时刻的秒杀活动列表      秒杀列表排序方式:进行中(未售罄) > 进行中(售罄) > 未开始          * 对于「进行中(未售罄)」:依次按商品人气值从高到低、商品ID从小到大排序          * 对于「进行中(已售罄)」:依次按最后卖出时间从晚到早、商品人气值从高到低、商品ID从小到大排序          * 对于「未开始」:依次按开始时间从早到晚、商品人气值从高到低、商品ID从小到大排序         * 对于已结束的秒杀,不返回。      返回秒杀活动id列表 商品拥有以下属性:     * 商品ID,32位非负整数      * 人气值,32位非负整数      * 库存,32位非负整数  现给出一串请求,每个请求的格式为:时间戳 函数名 参数。请对每个请求都输出其返回结果 (请求已经按照时间先后顺序排序过) 。 数据范围:     * 商品数量 N <= 10,000     * 请求数量 M <= 10,000     * add 数量 A <= 1,000     * buy 数量 B <= 10,000      * list 数量 L <= 100 输入描述: 第一行是两个整数 N 和 M ,分别表示商品数量和请求数量。 接下来有 N 行,每行表示一个商品,具体格式为:3个整数(被空格分隔)分别表示商品ID,人气值,库存 接下来有 M 行,每行表示一个请求,请求已经按时间戳从小到大排序。 具体格式为:时间戳 请求类型 请求参数... * 时间戳:正整数 * 请求类型,共三种:"add", "buy" 和 "list" * 请求参数:按题目描述中的顺序,参数之间空格分隔 输出描述: 对每个请求,输出其返回值,一个请求的输出占一行: * add:成功输出id,失败输出-1 * buy:成功输出0,失败输出-1 * list:输出活动id列表,按要求的顺序,相邻数字之间用一个空格分隔。若列表为空,则输出空行。 示例1 输入 6 13 1001 1 10 1002 1 10 1003 2 10 1004 2 10 1005 2 10 1006 3 10 1 add 2 20 1001 10 2 buy 0 1 3 buy 0 10 4 add 5 6 1002 2 5 list 6 buy 1 1 7 add 10 20 1003 11 8 add 10 20 1003 8 9 add 10 20 1004 3 10 add 11 20 1005 5 11 add 20 30 1006 1 12 buy 3 3 13 list 输出 0 0 -1 1 0 1 -1 -1 2 3 4 5 0 2 4 0 3 5
点赞 回复 分享
发布于 2017-09-02 17:21
import java.util.ArrayList;import java.util.Scanner;public class Main { public static goods[] goodsList; public static ArrayList <activity> activityId; public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) {//注意while处理多个case int n = in.nextInt(); // goods int m = in.nextInt(); // requests int gid; int peple; int limitQuantity; goodsList = new goods[n]; for (int i = 0; i < n; i++) { gid = in.nextInt(); peple = in.nextInt(); limitQuantity = in.nextInt(); goods g = new goods(gid, peple, limitQuantity); goodsList[i] = g; } for (int i = 0; i < m; i++) { int currentStamp = in.nextInt(); String requestType = in.next(); switch (requestType){ case "add": { int res; int st = in.nextInt(); int et = in.nextInt();int gi = in.nextInt(); int limit = in.nextInt(); if (st > currentStamp && st < et) { res = addActivity(st, et, gi, limit); } else res = -1; System.out.println(res); } case "buy": { int res; int activid = in.nextInt(); int quantity = in.nextInt(); res = buyGoods(currentStamp,activid, quantity); System.out.println(res); } case "list": { ArrayList<activity> activiId = getActivityList(currentStamp); for (activity a: activiId ) { System.out.print(a.id+ " "); } } } } } } public static int addActivity(int st, int et,int gid, int Quantity){ if (!activityId.isEmpty()){ for (activity a: activityId ) { if (a.go.gid == gid){ // 已经加入过了,返回某值 return -1; }else { goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ goods g = goodsList[i]; g.limitQuantity -= Quantity; // 修改商品参加活动后剩余库存 gForAct = new goods(gid, g.peple, Quantity); } } activityId.add(new activity(st, et, Quantity, gForAct)); return 1; } } }else { goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ goods g = goodsList[i]; g.limitQuantity -= Quantity; gForAct = new goods(gid, g.peple, Quantity); } } activityId.add(new activity(st, et, Quantity, gForAct)); return 1; } return -1; } public static int buyGoods(int cuTime, int activid, int quantity){ if (activityId.isEmpty()){ return -1; }else { for (activity a: activityId ) { if (a.id == activid){ if (a.eti > cuTime){ if (quantity < a.go.limitQuantity){ a.go.limitQuantity -= quantity; return 1; }else return -1; // 没有合并判断是为了根据情况返回不同值,忘记题目了。。 }else return -1; }else return -1; } } return -1; } public static ArrayList getActivityList(int cuTime){ if (activityId.isEmpty()) return null; for (activity a:activityId ) { if (a.eti < cuTime){ // 活动a结束 } else { if (a.go.limitQuantity < 0){ // 活动a售罄 } else{ // 活动a在售 } } } // 按规定多级排序 activityId return activityId; } static class goods{ // 商品 private int gid; private int peple; private int limitQuantity; public goods(int g, int p, int l){ gid = g; peple = p; limitQuantity = l; } } static class activity{ // 秒杀活动 private goods go; private int id = 0; ; private int eti; private int sti; public activity(int st, int et, int Quantity, goods g){ id ++; go = g; go.limitQuantity = Quantity; sti = st; eti = et; } } }
点赞 回复 分享
发布于 2017-09-02 18:12
算了将就吧,吃饭回来再改吧,中规中矩,有好思路的大神求教!
点赞 回复 分享
发布于 2017-09-02 18:13
写出来的了,格式不管它了,最后一个list不知道为什么读不出来,13读出来就打印完了,不想弄了
点赞 回复 分享
发布于 2017-09-02 21:58
import java.util.*; public class Main { public static goods[] goodsList; public static ArrayList <activity> activityId; public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { /*//注意while处理多个case*/ int n = in.nextInt(); /*// goods*/ int m = in.nextInt(); /*// requests*/ int gid; int popular; int limitQuantity; goodsList = new goods[n]; activityId = new ArrayList<>(); for (int i = 0; i < n; i++) { gid = in.nextInt(); popular = in.nextInt(); limitQuantity = in.nextInt(); goods g = new goods(gid, popular, limitQuantity); goodsList[i] = g; } System.out.println(); for (int i = 0; i < m; i++) { int currentStamp = in.nextInt(); String requestType = in.next(); // 最后一个list读不出来,只能使用"list "在其后加一个空格才能读 switch (requestType){ case "add": { int res; int st = in.nextInt(); int et = in.nextInt(); int gi = in.nextInt(); int limit = in.nextInt(); if (st > currentStamp && st < et) { res = addActivity(st, et, gi, limit); } else res = -1; System.out.println(res); break; } case "buy": { int res; int activid = in.nextInt(); int quantity = in.nextInt(); res = buyGoods(currentStamp, activid, quantity); System.out.println(res); break; } case "list": { ArrayList<activity> activiId = getActivityList(currentStamp); if (activiId.isEmpty() || activiId == null) {System.out.println();} else { for (activity a: activiId ) { System.out.print(a.id+ " "); } System.out.println(); } break; } } } } in.close(); } public static int addActivity(int st, int et,int gid, int Quantity){ if (!activityId.isEmpty()){ for (activity a: activityId ) { if (a.go.gid == gid){ /*// 每个goodsId至多添加一次秒杀活动*/ return -1; } } goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ /*// goodsId 一定为存在的商品id。*/ if (goodsList[i].limitQuantity >= Quantity){ goods g = goodsList[i]; g.limitQuantity -= Quantity; /*// 修改商品参加活动后剩余库存*/ gForAct = new goods(gid, g.popular, Quantity); activity act = new activity(st, et, Quantity, gForAct); activityId.add(act); return act.id; }else { /*// 秒杀商品数量大于商品库存,添加失败,返回-1*/ return -1; } } } }else { goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ /*// goodsId 一定为存在的商品id。*/ if (goodsList[i].limitQuantity >= Quantity){ goods g = goodsList[i]; g.limitQuantity -= Quantity; /*// 修改商品参加活动后剩余库存*/ gForAct = new goods(gid, g.popular, Quantity); activity act = new activity(st, et, Quantity, gForAct); activityId.add(act); return act.id; }else { return -1; } } } } return -1; } public static int buyGoods(int cuTime, int activid, int quantity){ if (activityId.isEmpty()){ return -1; }else { for (activity a: activityId ) { if (a.id == activid){ /*// activityId 一定是请求时存在的活动id*/ if (a.eti > cuTime && a.sti <= cuTime){ if (quantity <= a.go.limitQuantity){ /*// 购买成功,减少库存,返回0*/ a.go.limitQuantity -= quantity; a.lastSoldStamp = cuTime; /*// 更新最近一次卖出时间*/ return 0; }else return -1; /*// 购买数量大于秒杀商品剩余数量,购买失败,返回-1*/ }else return -1; /*// 秒杀未开始或已结束,购买失败,返回-1*/ } } } return -1; } public static ArrayList getActivityList(int cuTime){ if (activityId.isEmpty()) return null; ArrayList<activity> unSoldOut = new ArrayList<>(); ArrayList<activity> soldOut = new ArrayList<>(); ArrayList<activity> unStart = new ArrayList<>(); for (activity a:activityId ) { if (a.eti > cuTime){ if (a.sti <= cuTime){ if (a.go.limitQuantity > 0){ unSoldOut.add(a); /*//进行中(未售罄)*/ }else soldOut.add(a); /*// 进行中(已售罄)*/ }else unStart.add(a); /*// 未开始*/ }/*// 对于已结束的秒杀,不返回*/ } unSoldOut.sort( comparator1); /*// 多级排序*/ soldOut.sort( comparator2); unStart.sort(comparator3); activityId.clear(); activityId.addAll(unSoldOut); activityId.addAll(soldOut); activityId.addAll(unStart); return activityId; } private final static Comparator comparator1 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a1 = (activity) o1; activity a2 = (activity) o2; int a = a2.go.popular - a1.go.popular; /*// 依次按商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.gid - a1.go.gid; if (a != 0) cr = (a > 0) ? -2 : 2; else cr = 0; } } return cr; } }; private final static Comparator comparator2 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a2 = (activity) o1; activity a1 = (activity) o2; int a = a2.lastSoldStamp - a1.lastSoldStamp; /*// 按最后卖出时间从晚(lastSoldStamp 大)到早(lastSoldStamp 小),商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.popular - a1.go.popular; if (a != 0) cr = (a > 0) ? 2 : -2; else { a = (a2.go.gid - a1.go.gid); if (a != 0) cr = (a > 0) ? 1 : -3; else { cr = 0; } } } } return cr; } }; private final static Comparator comparator3 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a2 = (activity) o1; activity a1 = (activity) o2; int a = a2.sti - a1.sti; /*// 依次按开始时间从早(sti 小)到晚(sti 大).商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.popular - a1.go.popular; if (a != 0) cr = (a > 0) ? 2 : -2; else { a = (a2.go.gid - a1.go.gid); if (a != 0) cr = (a > 0) ? 1 : -3; else { cr = 0; } } } } return cr; } }; static class goods{ /*// 商品*/ private int gid; private int popular; private int limitQuantity; public goods(int g, int p, int l){ gid = g; popular = p; limitQuantity = l; } } static class activity{ /*// 秒杀活动*/ private goods go; private static int num = 0; private final int id = num++; private int eti; private int sti; private int lastSoldStamp; public activity(int st, int et, int Quantity, goods g){ go = g; go.limitQuantity = Quantity; sti = st; eti = et; lastSoldStamp = eti-1; /*// 初始化最近一次卖出时间为最晚卖出*/ } } }
点赞 回复 分享
发布于 2017-09-03 13:42

相关推荐

one_t:硕还是本?什么岗
点赞 评论 收藏
分享
11-09 14:54
已编辑
华南农业大学 产品经理
大拿老师:这个简历,连手机号码和照片都没打码,那为什么关键要素求职职位就不写呢? 从上往下看,都没看出自己到底是产品经理的简历,还是电子硬件的简历? 这是一个大问题,当然,更大的问题是实习经历的描述是不对的 不要只是去写实习流程,陈平,怎么去开会?怎么去讨论? 面试问的是你的产品功能点,是怎么设计的?也就是要写项目的亮点,有什么功能?这个功能有什么难处?怎么去解决的? 实习流程大家都一样,没什么优势,也没有提问点,没有提问,你就不得分 另外,你要明确你投的是什么职位,如果投的是产品职位,你的项目经历写的全都是跟产品无关的,那你的简历就没用 你的面试官必然是一个资深的产品经理,他不会去问那些计算机类的编程项目 所以这种四不像的简历,在校招是大忌
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务