Adventure Time - Finn 3
본문 바로가기
코테연습/python

두 리스트 합치기

by hyun9_9 2026. 4. 13.

오름차순으로 정렬이 된 두 리스트가 주어지면 두 리스트를 오름차순으로 합쳐 출력하는 프로 그램을 작성하세요

import sys
# sys.stdin=open("input.txt","rt")
# n,m = map(int,input().split())
# n = int(input())
# arr = list(map(int,input().split()))
# n = input()

n = int(input())
arrN = list(map(int,input().split()))
m = int(input())
arrM = list(map(int,input().split()))
res = []

# print(sorted(arrN+arrM)) # 파이썬 내장함수

indexN = 0
indexM = 0
for i in range(n+m):
    if indexN < n and indexM < m:
        if arrN[indexN] < arrM[indexM]:
            res.append(arrN[indexN])
            indexN +=1
        else:
            res.append(arrM[indexM])
            indexM +=1
    else:
        if n > m:
            res.append(arrN[indexN])
            indexN +=1
        else:
            res.append(arrM[indexM])
            indexM +=1
for i in res: print(i, end=" ")

'코테연습 > python' 카테고리의 다른 글

격자판 최대합  (0) 2026.04.13
수들의 합  (0) 2026.04.13
카드 역배치(정올 기출)  (0) 2026.04.12
숫자만 추출  (0) 2026.04.12
회문 문자열 검사  (0) 2026.04.12