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
풀이
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i < n; i++){
arr[i] = in.nextInt();
}
in.close();
String result = "";
for (int i = arr.length; i > 0; i--){
result += String.valueOf(arr[i-1]) +" ";
}
System.out.println(result);
}
}
처음에 reverse를 이용해서 풀었었는데, reverse는 char처럼 문자 하나하나를 뒤엎는 문제에서만 사용해야 할 듯
10을 넣으면 01로 출력한다
'Algorithms > 30 Days of Code' 카테고리의 다른 글
Day 9: Recursion (0) | 2018.02.19 |
---|---|
Day 8: Dictionaries and Maps (0) | 2018.02.19 |
Day 6: Let's Review (0) | 2018.02.14 |
Day 5: Loops (0) | 2018.02.13 |
Day 4: Class vs. Instance (0) | 2018.02.13 |