Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class TaskA {
- public static Stack<Car> stack = new Stack<>();
- public static void add(int count, String load) {
- stack.push(new Car(count, load));
- }
- public static void delete(int count) {
- while (count!=0) {
- if (stack.peek().count<count) {
- count-=stack.peek().count;
- stack.pop();
- } else {
- stack.peek().count-=count;
- count=0;
- }
- }
- }
- public static int get(String load) {
- int count = 0;
- for (int i = 0; i < stack.size(); i++) {
- if (stack.get(i).load.equals(load)) {
- count+=stack.get(i).count;
- }
- }
- return count;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- for (int i = 0; i<=n; i++) {
- String str=in.nextLine();
- String[] split = str.split(" ");
- if (split[0].equals("add")) {
- add(Integer.parseInt(split[1]), split[2]);
- continue;
- }
- if (split[0].equals("delete")) {
- delete(Integer.parseInt(split[1]));
- continue;
- }
- if (split[0].equals("get")) {
- System.out.println(get(split[1]));
- continue;
- }
- }
- }
- public static class Car {
- int count;
- String load;
- public Car(int count, String load) {
- this.count=count;
- this.load=load;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement