I tried to solve 10 selected past questions that should be solved after registering with AtCoder with Java, Stream API

Introduction

What to do next after registering with AtCoder-If you solve this much, you can fight enough! The 10 questions introduced in the past 10 selected questions ~ were solved using Java8 and Stream API. Click here for AtCoder Beginners Selection I did it to study Stream API, so I want to feed on Masakari.

Premise

--Performance is ignored for the time being, but input is received by BufferedReader --Avoid procedural writing as much as possible

ABC086A Product

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        System.out.println(
                Arrays.stream(input.split(" "))
                        .mapToInt(Integer::parseInt)
                        .reduce(1,(x, y) -> x * y % 2) != 0 ? "Odd":"Even"
        );
    }
}

ABC081A Placing Marbles

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        System.out.println(
                Arrays.stream(input.split(""))
                        .filter(i -> i.equals("1"))
                        .count()
        );
    }
}

ABC081B Shift only

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        int[] intArray = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        for(int count = 0; ; count++) {
            if(Arrays.stream(intArray).allMatch(i -> i % 2 == 0)) {
                intArray = Arrays.stream(intArray).map(i -> i / 2).toArray();
            } else {
                System.out.println(count);
                break;
            }
        }
    }
}

Due to the convenience of using BufferedReader, it was easy to receive it in a row, divide it into an array and convert it to Stream, so even if I receive ʻinput`, I will not use it

ABC087B Coins

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.IntStream;

class Main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int A = Integer.parseInt(br.readLine());
        int B = Integer.parseInt(br.readLine());
        int C = Integer.parseInt(br.readLine());
        int X = Integer.parseInt(br.readLine());

        final long count = IntStream.rangeClosed(0, A).map(i -> i * 500)
                        .flatMap(i -> IntStream.rangeClosed(0, B).map(j -> i + 100 * j))
                        .flatMap(i -> IntStream.rangeClosed(0, C).map(j -> i + 50 * j))
                        .filter(i -> i == X)
                        .count();

        System.out.println(count);
    }
}

ABC083B Some Sums

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Main {
 
    public static void main(String args[]) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] nab = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
 
        final long count = IntStream.rangeClosed(1, nab[0]).filter(num -> {
            int sum = Arrays.stream(String.valueOf(num).split("")).mapToInt(Integer::parseInt).sum();
            return sum >= nab[1] && sum <= nab[2];
        })
        .sum();

        System.out.println(count);
    }
}

ABC088B Card Game for Two

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
 
class Main {
    public static void main(String[] args) throws java.lang.Exception {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        br.readLine();
 
        List<Integer> list = Stream.of(br.readLine().split(" "))
                .map(Integer::parseInt)
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
 
        int alice = IntStream.range(0, list.size())
                .filter(i -> i % 2 == 0)
                .mapToObj(i -> list.get(i))
                .mapToInt(Integer::intValue)
                .sum();
 
        int bob = IntStream.range(0, list.size())
                .filter(i -> i % 2 == 1)
                .mapToObj(i -> list.get(i))
                .mapToInt(Integer::intValue)
                .sum();
 
        System.out.println(alice - bob);
    }
}

Don't use ʻinput` for the same reason as ShiftOnly

ABC085B Kagami Mochi

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) throws java.lang.Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        long count = br.lines().limit(N).collect(Collectors.toList()).stream().distinct().count();
        System.out.println(count);

    }

}

ABC085C Otoshidama

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.stream.IntStream;

class Main {
  public static void main(String[] args) throws java.lang.Exception {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int[] intArray = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    int n = intArray[0];
    int y = intArray[1];

    IntStream.rangeClosed(0, n).forEach(ichiman -> {
      IntStream.rangeClosed(0, n -ichiman).forEach(gosen -> {
        int senen = n -ichiman -gosen;
        if(10000 *ichiman + 5000 *gosen + 1000 *senen == y) {
          System.out.println(ichiman +" " +gosen +" " +senen);
          System.exit(0);
        }
      });
    });

    System.out.println("-1 -1 -1");
  }
}

ABC049C Daydream

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Stream;

class Main {
    public static void main(String[] args) throws java.lang.Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();

        long count = Stream.of(S).map(i -> i.replaceAll("eraser", "x"))
                    .map(i -> i.replaceAll("erase", "x"))
                    .map(i -> i.replaceAll("dreamer", "x"))
                    .map(i -> i.replaceAll("dream", "x"))
                    .map(i -> i.replaceAll("x", ""))
                    .filter(i -> i.equals(""))
                    .count();

        System.out.println(count > 0 ? "YES" : "NO");

    }
}

ABC086C Traveling

Is this used?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class Main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        List<List<Integer>> plans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            List<Integer> plan = Stream.of(br.readLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
            plans.add(plan);
        }
 
        int diff = 0;
        int nextX = 0;
        int nextY = 0;
 
        for (List<Integer> plan: plans) {
            diff = plan.get(0) - diff;
            nextX = plan.get(1) - nextX;
            nextY = plan.get(2) - nextY;
            int dist = Math.abs(nextX) + Math.abs(nextY);
            if (diff < dist) {
                System.out.println("No");
                return;
            } else {
                if ((diff - dist) % 2 != 0) {
                    System.out.println("No");
                    return;
                }
            }
        }
 
        System.out.println("Yes");
 
    }
}

Recommended Posts

I tried to solve 10 selected past questions that should be solved after registering with AtCoder with Java, Stream API
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I want to write a loop that references an index with Java 8's Stream API
I tried to interact with Java
I tried using Java8 Stream API
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
I tried to make a Web API that connects to DB with Quarkus
I tried to summarize the Stream API
I tried to create a method to apply multiple filters at once with Java Stream API. Is this okay?
I tried to break a block with java (1)
I tried what I wanted to try with Stream softly.
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
[Java] I tried to solve Paiza's B rank problem
[For beginners] How to operate Stream API after Java 8
I tried to draw animation with Blazor + canvas API
I tried to implement Stalin sort with Java Collector
[Java] I tried to implement Yahoo API product search
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I tried to link chat with Minecraft server with Discord API
Convert 2D array to csv format with Java 8 Stream API
I tried to be able to pass multiple objects with Ractor
[Java] Introduction to Stream API
I tried to solve the problem of "multi-stage selection" with Ruby
A story that I struggled to challenge a competition professional with Java
I tried to make an Android application with MVC now (Java)
Introduction to Java that can be understood even with Krillin (Part 1)