[구름톤 챌린지 WEEK 1 - 구현] DAY 1 ~ 3 리뷰
DAY1. 운동 중독 플레이어
- 풀이
import math
w, r = map(int, input().split())
print(math.floor(w * (1 + r / 30)))
- 정해
import math
W, R = map(int, input().split())
print(math.trunc(W * (1 + R / 30)))
- 시도한 코드
w, r = map(int, input().split())
RM = w * (1 + r / 30)
print(f'{RM:.0f}')
주어진 1, 2번 테케에서는 결과값이 같아 통과하였지만 제출 후 5개의 테케에서 실패한 이유가 궁금해 찾아보았다.
찾아보니 f 자체가 float 표현이다.
보이는 출력물 자체는 정수로 바뀌어보이는데
f-strings 표현은 string이므로
int로 변환해 주어도. 0이 찍히는 소수 표현 방식이기 때문에 소수점을 없애는 방식으로 사용할 수 없다.
문제 풀이에서는 성공하는 테케가 있고, 실패하는 테케가 있어 이 점이 궁금했는데
스터디원분께 물어보니 궁금증이 해결되었다.
2번째 표현 방식을 쓰면 반올림이 되고
3번째 표현 방식을 쓰면 반내림이 된다.
math.trunc() 같은 방식이 아니라 반올림 방식으로 테케가 성공했다 실패했다 한 것!
작은 궁금증이지만 해결되니 시원하다.
파이썬 소수점 다루는 방식은 따로 정리해 놨다.
2023.08.16 - [프로그래밍 언어/Python] - Python 소수점 다루기 - math, int(), //1
Python 소수점 다루기 - math, int(), //1
소수점 내리는 방법 int() //1 math.floor() math.tunc() 양수에서는 위 방법 모두 같은 숫자 값을 보여준다. print(int(7.12)): 7 print(math.floor(7.12)): 7 print(7.12//1): 7.0 print(int(3.1415)): 3 print(math.floor(3.1415)): 3 print(3
hi-rachel.tistory.com
참고:
- f-strings 표현 설명
https://realpython.com/python-f-strings/
Python 3's F-Strings: An Improved String Formatting Syntax (Guide) – Real Python
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster! By the end of this article, you will learn how and why to sta
realpython.com
DAY 2. 프로젝트 매니징
- 풀이
n = int(input())
t, m = map(int, input().split())
time = 0
for _ in range(n):
time += int(input())
t += time // 60
m += time % 60
while m >= 60:
m -= 60
t += 1
while t >= 24:
t -= 24
print(t, m)
- 정해
n = int(input())
t, m = map(int, input().split())
c = [int(input()) for i in range(n)]
time = (t * 60 + m + sum(c)) % 1440
hour = time // 60
minute = time % 60
print(hour, minute)
list comprehension과 모두 분 단위로 고치고 한 번에 더해준 후, % 1440분으로 나눈 나머지를 가져오는 방식이 배울 점이었다.
DAY 3. 합 계산기
- 풀이
t = int(input())
result = 0
for _ in range(t):
a, operator, b = input().split(' ')
a = int(a)
b = int(b)
if operator == "+":
result += a + b
elif operator == "-":
result += a - b
elif operator == "/":
result += a // b
elif operator == "*":
result += a * b
print (result)
- 정해
result = 0
T = int(input())
for i in range(T):
s = input().split()
firstNum = int(s[0])
command = s[1]
secondNum = int(s[2])
if command == "+":
result += firstNum + secondNum
elif command == "-":
result += firstNum - secondNum
elif command == "*":
result += firstNum * secondNum
else:
result += firstNum // secondNum
print(result)
// 계산으로 소수점을 버리는 점을 주의해야 하는 문제였다.