Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Deque;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- import java.util.Scanner;
- public class WormsAndHoles {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Reading worms and holes
- String[] wormsInput = scanner.nextLine().split(" ");
- String[] holesInput = scanner.nextLine().split(" ");
- Deque<Integer> worms = new ArrayDeque<>();
- Queue<Integer> holes = new LinkedList<>();
- // Filling the stack with worms
- for (String worm : wormsInput) {
- worms.push(Integer.parseInt(worm));
- }
- // Filling the queue with holes
- for (String hole : holesInput) {
- holes.offer(Integer.parseInt(hole));
- }
- int matchesCount = 0;
- // Process worms and holes
- while (!worms.isEmpty() && !holes.isEmpty()) {
- int worm = worms.pop();
- int hole = holes.poll();
- if (worm == hole) {
- matchesCount++;
- } else if (worm > hole) {
- worm -= 3;
- if (worm > 0) {
- worms.push(worm);
- }
- }
- }
- // Output results
- if (matchesCount > 0) {
- System.out.println("Matches: " + matchesCount);
- } else {
- System.out.println("There are no matches.");
- }
- if (worms.isEmpty()) {
- if (matchesCount == wormsInput.length) {
- System.out.println("Every worm found a suitable hole!");
- } else {
- System.out.println("Worms left: none");
- }
- } else {
- System.out.print("Worms left: ");
- List<Integer> remainingWorms = new ArrayList<>(worms);
- Collections.reverse(remainingWorms);
- for (int i = 0; i < remainingWorms.size(); i++) {
- System.out.print(remainingWorms.get(i));
- if (i < remainingWorms.size() - 1) {
- System.out.print(", ");
- }
- }
- System.out.println();
- }
- if (holes.isEmpty()) {
- System.out.println("Holes left: none");
- } else {
- System.out.print("Holes left: ");
- List<Integer> remainingHoles = new ArrayList<>(holes);
- for (int i = 0; i < remainingHoles.size(); i++) {
- System.out.print(remainingHoles.get(i));
- if (i < remainingHoles.size() - 1) {
- System.out.print(", ");
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement