알고리즘

section3.4 두 리스트 합치기

gitofjy 2022. 10. 26. 23:49

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

 

 

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

result = []
result = flist + slist
print(*sorted(result))

 

정답으로 별생각 없이 풀었는데 포인터를 이용하는 문제였다

이번 주말에 한 번 더 풀어봐야겠다

 

import sys
sys.stdin = open("input.txt", "rt")
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
p1 = p2 = 0
c= []

while p1<n and p2<m:
    if a[p1] <= b[p2]:
        c.append(a[p1])
        p1 += 1
    else:
        c.append(b[p2])
        p2 += 1

if p1 < n:
    c = c + a[p1:]
if p2 < m:
    c = c + b[p2:]

for x in c:
    print(x,  end=' ')
728x90