본문 바로가기

프로그래밍/BOJ_Python 목표는 Diamond

[2475, 2920, 10172 | python] 검증수, 음계, 개

🎉문제 총평

solved.ac에서 가장 기본적인 class1을 통과하기 위해 남은 마지막 3문제!

 


[2475] 검증수

주어진 5개의 숫자들을 각각 제곱하고 합한 뒤 10으로 나눈 나머지를 출력한다.

try1: runtime error!

import numpy

nums = map(int, input().split())

sqrt_nums = np.sqrt(nums)

sum_sqrt = np.sum(sqrt_nums)

print(sum_sqrt//10)

 

 numpy를 np로 정의하지 않음

nums가 리스트가 아니었다?!

map 객체였던 map

 

try2: 런타임 에러!

위에서 문제라고 생각한 부분들을 고쳤는데도 여전히 런타임 에러가 나왔다.

import numpy as np

nums = list(map(int, input().split()))

sqrt_nums = np.sqrt(nums)

sum_sqrt = np.sum(sqrt_nums)

print(sum_sqrt//10)

 

검색해보고 에러를 다시한번 확인해봤다.

...확실히 보자

 

try3: numpy 빼고 재도전!

휴...

nums = list(map(int, input().split()))
sum = 0

for i in range(len(nums)):
    sum += nums[i]**2
    
print(sum%10)

 

찾아보니 리스트 컴프리헨션을 사용해 한줄로 작성할 수도 있었다. 

물론 가독성은 좀 떨어지지만...😥

print(sum([n**2 for n in map(int, input().split())]) % 10)

https://jinho-study.tistory.com/314

 

백준 알고리즘 2475번 검증수(python)

간단한 사칙연산 문제이다. res = 0 for n in list(map(int, input().split())): res += n**2 print(res%10) 더 짧게 짠 코드 print(sum([n**2 for n in map(int, input().split())]) % 10)

jinho-study.tistory.com

 


[2920] 음계

1~8까지의 숫자들의 순서에 대해 판단하는 문제

입력값이 순서대로인 숫자가 아니었으면 어려웠을 것 같다.

다행히 1,2,3,4,5,6,7,8이라 쉽게 풀었다.

 

nums = list(map(int, input().split()))

if nums == sorted(nums):
    print("ascending")
elif nums == sorted(nums, reverse=True):
    print('descending')
else:
    print("mixed")

[10172] 개

 

개 출력하기.

# 10172

print("|\_/|")
print("|q p|   /}")
print('( 0 )"""\\')
print('|"^"`    |')
print('||_/=\\\__|')