Algorithm/Prob
Py) SWEA 14361 숫자가 같은 배수
헬린인형
2022. 11. 6. 00:59
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
** permutation을 잊지 말자 **
1차: 11분 (고민하다가 시간이 너무 늦어서 답을 보기로..)
0. 입출력
1. 김예고리즘
아주 잠깐 몇 중 for문을 돌려야하나 생각했다. -> 순열 개념은 생각했으나 그게 순열이라고 바로 떠올리지 못함.
2. Solution
2-1. 사용 자료구조 및 알고리즘: x
2-1-1. 핵심 아이디어: 순열 Permutations
2-2. 로직
1) 주어진 수를 한 글자씩 입력 받는다
2) 순열을 이용해 한가지 경우씩 비교
3) 원래 값보다 크고, 원래 값으로 나눴을때 딱 떨어진다면 possible
4) 그렇지 않다면 impossible
2-3. 코드
from itertools import permutations
T = int(input())
for i in range(1, T+1):
data = list(map(int, input()))
original = int(''.join(map(str, data)))
result = 'impossible'
for value in permutations(data, len(data)):
if int(''.join(map(str, value))) > original:
if int(''.join(map(str, value))) % original:
result = 'possible'
break
print('#%d %s' %(i, result))
3. 피드백
from itertools import permutations 의 permutation을 잊지 말자..
for value in permutations(data, len(data)):
내일 안늦게 빨리 자자..
반응형