반응형
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, A, of N integers, print A's elements in reverse order as a single line of space-separated numbers.
Input Format
The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers describing array A's elements.
Constraints
- 1 <= N <= 1000
- 1 <= Ai <= 100000, where Ai 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
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();
}
int[] arrResult = new int[n];
for (int tmp = 0; tmp < arr.length; tmp++)
arrResult[n - tmp - 1] = arr[tmp];
for (int tmps = 0; tmps < arrResult.length; tmps++) {
System.out.print(arrResult[tmps]);
if(tmps != arrResult.length-1)
System.out.print(" ");
in.close();
}
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[HackerRank] Day 9: Recursion (0) | 2023.04.17 |
---|---|
[HackerRank] Day 8: Dictionaries and Maps (0) | 2023.04.17 |
[HackerRank] Day 6: Let's Review (0) | 2023.04.17 |
[HackerRank] Day 5: Loops (0) | 2023.04.17 |
[HackerRank] Day 4: Class vs. Instance (0) | 2023.04.17 |