关注
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
相关推荐
牛客热帖
正在热议
# 25届秋招总结 #
263774次浏览 2185人参与
# 0offer是寒冬太冷还是我太菜 #
887331次浏览 7909人参与
# 北方华创开奖 #
23659次浏览 260人参与
# 地方国企笔面经互助 #
2846次浏览 7人参与
# 学历or实习经历,哪个更重要 #
43420次浏览 328人参与
# 选完offer后,你后悔学本专业吗 #
13224次浏览 94人参与
# 查收我的offer竞争力报告 #
19502次浏览 255人参与
# 应届生被毁约被毁意向了怎么办 #
28185次浏览 243人参与
# 你最想要的公司福利是? #
41848次浏览 146人参与
# 如何一边实习一边秋招 #
987335次浏览 12609人参与
# 一觉醒来,我觉醒了超级打工人系统 #
3302次浏览 36人参与
# 嵌入式转岗的难度怎么样 #
11198次浏览 251人参与
# 你最希望上岸的公司是? #
76523次浏览 469人参与
# 如何写一份好简历 #
605005次浏览 8509人参与
# 面试体验感最好的是哪家? #
83644次浏览 818人参与
# 机械应届生薪资要多少才合适? #
12539次浏览 61人参与
# 牛客十周岁生日快乐 #
48733次浏览 759人参与
# 你认为第一份工作重要吗 #
5445次浏览 49人参与
# 985本硕1个中小厂offer,摆烂or继续努力 #
79927次浏览 589人参与
# 秋招OC许愿 #
228173次浏览 1880人参与
# 来聊聊机械薪资天花板是哪家 #
65725次浏览 445人参与