728x90
320x100
package chap02;
import java.util.Arrays;
import java.util.Scanner;
public class ReverseArray {
//배열 a의 요소를 역순으로 정렬
static void reverse(int[] a) {
for (int i = 0; i < a.length / 2; i++){
swap(a, i, a.length - i - 1);
}
}
// 배열 요소의 값을 바꿈
static void swap(int[] a, int idx1, int idx2) {
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("요솟수 : ");
int num = scan.nextInt(); //요솟수
int[] x = new int[num]; //요솟수가 num인 배열
for(int i = 0; i < num; i++){
System.out.print("x[" + i + "] : ");
x[i] = scan.nextInt();
}
reverse(x); //배열 a의 요소를 역순으로 정렬.
System.out.println("요소를 역순으로 정렬했습니다.");
System.out.println("x = " + Arrays.toString(x));
}
}
728x90
320x100
'💻 하나씩 차곡차곡 > 자료구조 & 알고리즘(JAVA)' 카테고리의 다른 글
배열요소를 복사하기 (chap02/Practice04) (0) | 2022.10.19 |
---|---|
배열 요소의 합계 구하기 (chap02/Practice03) (0) | 2022.10.18 |
배열요소의 최댓값 구하기 (chap02/MaxOfArray) (0) | 2022.10.16 |
for문을 이용하여 각종 별찍기 (chap01/TriangleLB) (2) | 2022.10.14 |
이중루프를 사용하여 구구단 곱셈표 출력하기 (chap01/Multi99Table) (1) | 2022.10.13 |