[BOJ] 16936 나3곱2

피누·2020년 10월 1일
0

문제 바로보기

몫을 구하는 연산때문에 계속 틀렸다. 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]
profile
어려운 문제를 함께 풀어가는 것을 좋아합니다.

0개의 댓글