BAEKJOON /15903:카드합체놀이

hyihyi·2022년 1월 11일
1

[python] 리스트의 요소를 지정된 함수로 처리해주는 map함수

  • map을 사용했을 때
>>> a = [1.2, 2.5, 3.7, 4.6]
>>> a = list(map(int, a))
>>> a
[1, 2, 3, 4]
  • map을 사용하지 않았을 때
>>> a = [1.2, 2.5, 3.7, 4.6]
>>> for i in range(len(a)):
...     a[i] = int(a[i])
...
>>> a
[1, 2, 3, 4]

<문제 설명>

-카드의 제일 작은 수 두개를 더하고 정렬하는 것을 총 합체 횟수만큼 한다.
-총 카드의 합을 구하는 프로그램을 작성하자

<문제 풀이>

card_count,combined_counts=input().split()
combined_counts=int(combined_counts) 	#str로 입력받은 card_count,
card_count=int(card_count)	     	#combined_counts를 int로 바꿔줌
str_list=()
str_list=input().split()
int_list=list(map(int,str_list))
int_list=sorted(int_list) #오름차순으로 정렬
for i in range(0,combined_counts):
    tmp=int_list[0]+int_list[1] #정렬한 것의 0,1번쨰를 더한 것을 tmp에 저장
    int_list[0]=tmp #tmp를 0번째
    int_list[1]=tmp #tmp를 1번째에 저장
sum=0
for i in range(0,card_count):
    sum=sum+int_list[i] #sum에다 리스트의 합을 더함
print(str(sum))
profile
자유롭게 쓴 나의 자유로운 Development voyage⛵

0개의 댓글