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.
--Performance is ignored for the time being, but input is received by BufferedReader
--Avoid procedural writing as much as possible
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"
);
}
}
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()
);
}
}
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
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);
}
}
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);
}
}
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
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);
}
}
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");
}
}
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");
}
}
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