[BOJ] 16936 나3곱2
몫을 구하는 연산때문에 계속 틀렸다. python3에서 /는 float division을 수행함으로 int(a/b)는 부정확한 몫을 리턴 할 수 있다. 때문에 floor division을 수행하는 a//b 로 수정한 후에야 ac를 받을 수 있었다.
참고 - What is the difference between a//b and int(a/b)?
--> 3의 갯수가 많고, 2의 갯수가 적은 순으로 정렬하면 되겠다
1. 원래 배열을 둔다.
2. 원래 배열과 인덱스를 공유하는 2의 갯수와 3의 갯수를 갖는 pair를 가지는 지료형 배열을 둔다
3. 정렬한다.
"""
import sys
class TwoThreeCnt:
def __init__(self, index, number):
self.index = index
self.two = TwoThreeCnt._get_cnt(number, 2)
self.three = TwoThreeCnt._get_cnt(number, 3)
@staticmethod
def _get_cnt(number, two_or_three):
cnt = 0
while True:
if number == 0:
return cnt
if number % two_or_three != 0:
return cnt
cnt += 1
number //= two_or_three
def solve(shuffled):
parsed = [TwoThreeCnt(idx, number) for idx, number in enumerate(shuffled)]
sort_seq = sorted(parsed, key=lambda item: (-item.three, item.two))
return [shuffled[item.index] for item in sort_seq]
if __name__ == '__main__':
n = int(sys.stdin.readline().rstrip())
inputs = sys.stdin.readline().rstrip().split()
answer = solve([int(input) for input in inputs])
[print(ans, end=' ') for ans in answer]
Author And Source
이 문제에 관하여([BOJ] 16936 나3곱2), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@litien/BOJ-16936-나3곱2저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)