Problem
Objective
Today, we're learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an array, , of integers, print 's elements in reverse order as a single line of space-separated numbers.
Input Format
The first line contains an integer, (the size of our array).
The second line contains space-separated integers describing array 's elements.
Constraints
- , where is the integer in the array.
Output Format
Print the elements of array in reverse order as a single line of space-separated numbers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
How I solved the problem
# for 문을 거꾸로 돌려야함 !
# for문 안에 초기화를 n - 1로 해주는것이 핵심. (index는 0부터 시작 ==== 최대 index는 개수보다 1이 작음)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); int[] arr = new int[n]; String[] arrItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < n; i++) { int arrItem = Integer.parseInt(arrItems[i]); arr[i] = arrItem; } String result = ""; for (int i = n - 1; i >= 0; i--) { result += " " + arr[i]; } result = result.substring(1); System.out.println(result); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2018.12.19] Dictionaries and Maps (0) | 2018.12.19 |
---|---|
[2018.12.18] Staircase (0) | 2018.12.18 |
[2018.12.17] Plus Minus (0) | 2018.12.17 |
[2018.12.17] Let's Review (0) | 2018.12.17 |
[2018.12.16] Loops (0) | 2018.12.17 |
댓글