[BOJ][11004] K번째 수
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/11004
2. 사용 알고리즘
정렬
3. 풀이
입력받은 배열 오름차순 정렬 후 k번째 수 출력
4. 소스 코드
4-1. C++
https://github.com/dev-aiden/problem-solving/blob/main/boj/11004.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>
using namespace std;
int arr[5000003];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, k; cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> arr[i];
sort(arr, arr + n);
cout << arr[k - 1] << "\n";
return 0;
}
4-2. JAVA
https://github.com/dev-aiden/problem-solving/blob/main/boj/11004.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; ++i) arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
System.out.println(arr[k - 1]);
}
}
Leave a comment