BOJ_11728_배열 합치기 (Java, C++)
BOJ_11728_배열 합치기 (Java, C++)
[Silver V] 배열 합치기 - 11728
성능 요약
메모리: 353940 KB, 시간: 1464 ms
분류
정렬, 두 포인터
제출 일자
2024년 12월 19일 21:22:18
문제 설명
정렬되어있는 두 배열 A와 B가 주어진다. 두 배열을 합친 다음 정렬해서 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 배열 A의 크기 N, 배열 B의 크기 M이 주어진다. (1 ≤ N, M ≤ 1,000,000)
둘째 줄에는 배열 A의 내용이, 셋째 줄에는 배열 B의 내용이 주어진다. 배열에 들어있는 수는 절댓값이 109보다 작거나 같은 정수이다.
출력
첫째 줄에 두 배열을 합친 후 정렬한 결과를 출력한다.
문제 풀이
리스트에 추가 후 오름차순 정렬하여 출력했다.
코드
Java코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Author: nowalex322, Kim HyeonJae
*/
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
public static void main(String[] args) throws Exception {
new Main().solution();
}
public void solution() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
List<Integer> list = new ArrayList<>();
st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
list.add(Integer.parseInt(st.nextToken()));
}
st = new StringTokenizer(br.readLine());
for(int i=0; i<M; i++){
list.add(Integer.parseInt(st.nextToken()));
}
Collections.sort(list);
StringBuilder sb = new StringBuilder();
for(int num : list){
sb.append(num).append(" ");
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
C++ 코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Author: nowalex322, Kim HyeonJae
*/
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define MOD 1000000007
#define INF LLONG_MAX
#define ALL(v) v.begin(), v.end()
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
void solve() {
int N, M;
cin >> N >> M;
vector<int> list;
for (int i = 0; i < N; i++) {
int num;
cin >> num;
list.push_back(num);
}
for (int i = 0; i < M; i++) {
int num;
cin >> num;
list.push_back(num);
}
sort(ALL(list));
for (int num : list) {
cout << num << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1; // 기본적으로 1번의 테스트 케이스를 처리
// cin >> tt; // 테스트 케이스 수 입력 (필요 시)
while (tt--) {
solve();
}
return 0;
}
This post is licensed under
CC BY 4.0
by the author.