5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AtCoder に登録したら解くべき精選過去問 10 問を Java,StreamAPIで解いてみた

Last updated at Posted at 2020-01-17

はじめに

AtCoder に登録したら次にやること ~ これだけ解けば十分闘える!過去問精選 10 問 ~ で紹介されていた10問をJava8, StreamAPIを使って解いた。
AtCoder Beginners Selectionはこちら
StreamAPIの勉強のためにやったのでマサカリを糧にしたい。

前提

  • パフォーマンスはとりあえず無視するが、入力はBufferedReaderで受け取る
  • できるだけ手続き的な書き方を避ける

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;
            }
        }
    }
}

BufferedReaderを使う都合で、行で受け取って配列に分割してStreamに変換するのがやりやすかったからinputを受け取っても使わない

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);
    }
}

ShiftOnlyと同じ理由で、inputを使わない

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

これ、使い所あるんですかね..

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");
 
    }
}
5
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?