반응형
문제
Supervin 이 특별한 계산기를 가지고 있다. 현재 N이 계산기에 표시되어있다. Supervin은 홀수를 싫어한단다. 계산기를 +,- 버튼만을 눌러서 짝수 번호만 표시되게 하려면 최소 몇번을 눌러야하는가?
풀이
일단 나는 재귀함수로 구현을 했다. 홀수인 수의 자릿수를 찾고나서 그 다음 자릿수들의 값을 (1) 더해서 자릿수를 바꾸거나 (2) 빼서 자릿수를 바꾸고 그 결과값을 비교한 후 더 작은 값을 반환하는 방식으로 풀었다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | T = int(input()) max_n = 10**16 def findOddNumber(n, cnt, t): eo = list(int(x) % 2 for x in n) # 모든 수가 짝수면 리턴 if 1 not in eo: return cnt for i in range(len(n)): if eo[i] == 0: continue # 자릿수 cipher = len(n) - i - 1 cipherN = int("1" + "0" * cipher) remain = 0 # 나머지 if len(n) > i+1: remain = int(n[i+1:]) else: return cnt + 1 res1 = max_n res2 = max_n # sol A if (t is "A" or t is "C") : need = cipherN - remain n1 = int(n) + need cnt1 = cnt + need res1 = findOddNumber(str(n1), cnt1, "A") # sol B if (t is "B" or t is "C") : n2 = int(n) - (remain + 2) cnt2 = cnt + (remain + 2) res2 = findOddNumber(str(n2), cnt2, "B") return min(res1, res2) for t in range(1, T+1) : ans = findOddNumber(input(), 0, "C") print("Case #{}: {}".format(t, ans)) | cs |
문제 출처 : https://code.google.com/codejam/contest/9234486/dashboard#s=p0
반응형
'0 > algorithm' 카테고리의 다른 글
백준 5052번 전화번호 목록 (Python) (0) | 2018.11.12 |
---|---|
백준 7576번 토마토 (Python) (0) | 2018.11.11 |
백준 2644번 촌수계산 (Python) (0) | 2018.11.04 |
백준 3273번 두수의 합 (Python) (0) | 2018.11.02 |
백준 11501번 주식 (Python) (0) | 2018.11.02 |