Objective
Today, we will learn about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video.
Task
Given an array, A, of N integers, print A's elements in reverse order as a single line of space-separated numbers.
Example
A = [1, 2, 3, 4]
Print 4 3 2 1. Each integer is separated by one space.
Input Format
The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers that describe array 's elements.
Constraints
- 1 <= N <= 1000
- 1 < A[i] <= 10000, where A[i] is the i(th) integer in the array.
Output Format
Print the elements of array A in reverse order as a single line of space-separated numbers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
- N길이의 A라는 공백으로 구분된 배열을 준다.
- 받은 배열을 뒤집어서 출력하여준다.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Collections.reverse(arr);
for(int num : arr) {
System.out.print(num + " ");
}
bufferedReader.close();
}
}
'알고리즘 > Hackerrank' 카테고리의 다른 글
[Hackerrank] Recursion 3 (0) | 2021.10.10 |
---|---|
[Hackerrank] Dictionaries and Maps (0) | 2021.10.05 |
[Hackerrank] Let's Review (0) | 2021.10.02 |
[Hackerrank] Loops (0) | 2021.09.29 |
[Hackerrank] Class vs. Instance (0) | 2021.09.27 |