파이썬 알고리즘 177번 | [백준 2745번] 진법변환

Yunny.Log ·2022년 6월 17일
0

Algorithm

목록 보기
180/318
post-thumbnail

177. 진법변환

1) 어떤 전략(알고리즘)으로 해결?

2) 코딩 설명

<내 풀이>


import sys

n,b = map(str, sys.stdin.readline().strip().split())
n=list(n)
b=int(b)
res = 0
strr="ABCDEFGHIJKLMNOPQRSTUWVXYZ"
res=0
for i in range(len(n)) :
    #print(i, n[i], len(n)-i-1 , res)
    # 1 2 3 이면 맨 처음 수는 자릿수는 2, 숫자는 인덱스 0 i
    if n[i] in strr : 
        
        res+=(ord(n[i])-55)*((b**(len(n)-i-1)))
    else : 
        #print(int(n[i]),(b**len(n)-i-1))
        res+=int(n[i])*((b**(len(n)-i-1)))
print(res)

다른 분의 초간단 풀이,,!

import sys
input = sys.stdin.readline

n, b = input().split()

print(int(n, int(b)))

결과

풀이

참고

<다른 분의 풀이 or 내 틀렸던 풀이, 문제점>

1차 시도


 for i in range(len(str(n))-1, -1, -1) :
     # i 는 자릿수 3 2 1
     print("i",i)
     #if n[i].isalpha : 
     if n[len(str(n))-i] in strr:
         k = ord((str(n)))-i-55
         res+= k*((b)**i)

     else : 
         print(int(len(str(n))-i),i,res)
         print(int(len(str(n))-i)*((b)**i))
         res+= int(len(str(n))-i)*((b)**i)

 print(res)

2차 시도

import sys

n,b = map(str, sys.stdin.readline().strip().split())
n=list(n)
b=int(b)
res = 0
strr="ABCDEFGHIJKLMNOPQRSTUVXYZ"
res=0
for i in range(len(n)) : 
# 123 이면 맨 처음 수는 n[i], 자릿수는 3, 숫자는 인덱스 0 = i
    if n[i] in strr : 
    #if n[i].isalpha :     
        res+=(ord(n[i])-55)*((b**(len(n)-i-1)))
    else : 
      
        res+=int(n[i])*((b**(len(n)-i-1)))
print(res)

=> 백준 2745 VALUE ERROR ? => 나는 저 STRR에 W를 빼먹음 ^^

<반성 점>

<배운 점>

  • int(받은 수, 변환 진수) => 10진법으로 출력이 잘 된다 !

0개의 댓글