프로그래밍/HackerRank
[HackerRank][Python3] Strange Counter
snoopybox
2018. 9. 7. 23:19
Problem :
https://www.hackerrank.com/challenges/strange-code/problem
My Solution :
#!/usr/bin/env python3
def strange_counter(t):
max_time = 0
i = 1
while max_time < t:
max_time = 3*(2**i - 1)
i += 1
return max_time - t + 1
t = int(input())
result = strange_counter(t)
print(result)
Comment :
위 풀이는 내가 문제를 처음 접했을 때 떠올린 방법이다. 고2 수학1 시간에 배웠던 등비수열의 합 공식을 활용한건데... 나중에 Discussions을 보니 합을 구하지 않고 t 에서 점점 빼가는 방식으로 접근한 사람이 있었다. 아래는 그 방식으로 푼 것.
My Solution2 :
#!/usr/bin/env python3
def strange_counter(t):
value = 3
while value < t:
t -= value
value *= 2
return value - t + 1
t = int(input())
result = strange_counter(t)
print(result)