本人编程小白,求大神赐教!😂
import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { int N = 0;//货物槽数 int M = 0;//操作人数 ArrayList<Stack> stackList = new ArrayList<>();//储存货物的stack,stack里面装着货物价格。[(1,1,1,1,1,1),stack,stack....] ArrayList<Integer> priceList = new ArrayList<>();//保存不同人的价格 Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); //把货物价格推进栈 for (int i = 0; i < N; i++){ stackList.add(new Stack()); int price = scanner.nextInt();//获取价格 for(int j =0; j <= 100; j++) { stackList.get(i).push(price);//推进栈 } } int left = 0;//左手货物 int right = 0;//右手货物 int price = 0;//总计价格 ArrayList<Integer> bag = new ArrayList<>(); //获取用户操作左右手,操作方式,操作对应货架号 for (int i = 0; i < M; i++){ int operationTimes = scanner.nextInt();//用户操作次数 price = 0; left = 0; right = 0; for (int j = 0; j < operationTimes; j++){ String hand = scanner.next();//左手或者右手 String action = scanner.next();//take, return 或者 keep int actionNum = 0; if (!action.equals("keep")){ actionNum = scanner.nextInt();//对应操作的货架号 } if (hand.equals("left")){ if (action.equals("take")){ left = actionNum; price += (int)(stackList.get(actionNum - 1).pop());//把价格从栈中pop出,总计价格增加 }else if (action.equals("return")){ price -= left; //总计价格减少 stackList.get(actionNum - 1).push(left); //把手上货物推进货物槽 left = 0; }else { bag.add(left); left = 0; } }else { if (action.equals("take")){ right = actionNum; price += (int)(stackList.get(actionNum - 1).pop()); }else if (action.equals("return")){ price -= right; stackList.get(actionNum - 1).push(right); right = 0; }else { bag.add(right); right = 0; } } } priceList.add(price); //每完成一个人的所有操作,把他的总价 加入list } for (int i = 0; i < priceList.size(); i++){ System.out.println(priceList.get(i));//所有人操作完成,打印价格表 } } }
全部评论
(1) 回帖