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; } } }
点赞 5

相关推荐

冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客企业服务