본문 바로가기

ALGORITHM/인프런코테

3-1 두 배열 합치기(자바)

첫 번째 배열의 크기 n이 주어집니다. n크기 만큼 배열에 입력받습니다.

두 번째 배열의 크기 m이 주어집니다. m크기 만큼 배열에 입력받습니다.

두 배열을 합쳐서 정렬한 후에 출력하면 됩니다.

 

문제풀이 : 배열1과 배열2를 arraylist에 넣은후에 컬렉션의 sort함수를 사용하여 정렬한 후에 출력하면 됩니다.

import java.util.*;

public class Main {

    static int[] arr1 = new int[100];
    static int[] arr2 = new int[100];
    static ArrayList<Integer> arr3 = new ArrayList<>();

    public static void main(String[] args){
        Main main = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        for(int i = 0; i < n; i++)
        {
            arr1[i] = in.nextInt();
        }



        int m = in.nextInt();

        for(int i = 0; i < m; i++)
        {
            arr2[i] = in.nextInt();
        }

        for(int i = 0; i < n; i++)
        {
           arr3.add(arr1[i]);
        }

        for(int i = 0; i < m; i++)
        {
            arr3.add(arr2[i]);
        }

        Collections.sort(arr3);

        for (Integer element : arr3) {
            System.out.print(element+" ");
        }


    }





}

'ALGORITHM > 인프런코테' 카테고리의 다른 글

53. k진수 출력  (0) 2023.08.02
38 inversion sequence  (0) 2023.07.19