Notice
Recent Posts
Recent Comments
«   2024/10   »
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
10-11 00:15
Archives
Today
Total
관리 메뉴

Developer_Neo

[백준] 알고리즘 2750. 수 정렬하기 With 자바 본문

프로그래밍/백준알고리즘

[백준] 알고리즘 2750. 수 정렬하기 With 자바

_Neo_ 2021. 9. 4. 18:29
반응형

문제

N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.

입력

첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 숫자가 주어진다. 이 수는 절댓값이 1,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.

출력

첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int i,j,min,temp;
		int index=0;
		int []arr=new int[1001];
		for(i=0;i<n;i++) {
			int n1=sc.nextInt();
			arr[i]=n1;
		}
		
		for(i=0;i<n;i++) {
			min=1001;
			for(j=i;j<n;j++) {
				if(min>arr[j]) {
					min=arr[j];
					index=j;
				}
			}
			temp=arr[i];
			arr[i]=arr[index];
			arr[index]=temp;
		}
		
		
		for(i=0;i<n;i++) {
			System.out.println(arr[i]);
		}
		
		
	}

}

min=1001로 안적으면 에러가 난다..

 

https://www.acmicpc.net/problem/2750

반응형
Comments