Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바편
- 페이징
- GIT
- 스프링부트핵심가이드
- 목록처리
- Kernighan의 C언어 프로그래밍
- 코드로배우는스프링웹프로젝트
- 네트워크 설정
- 코드로배우는스프링부트웹프로젝트
- iterator
- 알파회계
- 스프링 시큐리티
- 자료구조와 함께 배우는 알고리즘 입문
- ㅒ
- 리눅스
- 자료구조와함께배우는알고리즘입문
- 선형대수
- 구멍가게코딩단
- resttemplate
- d
- 서버설정
- 처음 만나는 AI 수학 with Python
- 처음 만나는 AI수학 with Python
- 데비안
- network configuration
- 티스토리 쿠키 삭제
- /etc/network/interfaces
- 친절한SQL튜닝
- baeldung
- 이터레이터
Archives
- Today
- Total
bright jazz music
배열을 역순으로 정렬하기 본문
import java.util.Scanner;
import java.util.Arrays;
public class ReverseArray {
static void swap(int[] a, int idx1, int idx2) {
// 실제로 위치를 교체
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
static void reverse(int[] a) {
for(int i=0; i<a.length / 2; i++){
//위치를 교체할 element의 인덱스를 반복해서 입력
swap(a, i, a.length-1-i);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("input array size: ");
int elmntNum = sc.nextInt();
int[] a = new int[elmntNum];
for(int i=0; i < a.length; i++) {
System.out.print("input elements: ");
int num = sc.nextInt();
a[i] = num;
}
//입력받은 배열을 대입
reverse(a);
//역순 출력
System.out.println(Arrays.toString(a));
}
}
/*
input array size: 10
input elements: 1
input elements: 2
input elements: 3
input elements: 4
input elements: 5
input elements: 6
input elements: 7
input elements: 8
input elements: 9
input elements: 10
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
*/
'Algorithm&Data structure' 카테고리의 다른 글
신고 결과 받기 (0) | 2022.04.19 |
---|---|
숫자 문자열과 영단어 (0) | 2022.04.13 |
Binary Search (이진검색) (0) | 2021.12.22 |
Comments