'16진수'에 해당되는 글 1건

1. 16진수

만든이 : 웃긴 전골

 

2. 문제정보

level.goorm.io/exam/43226/16%EC%A7%84%EC%88%98/quiz/1

 

구름LEVEL

코딩테스트에서 가장 높은 비중을 차지하는 알고리즘 문제를 제작하고 풀이할 수 있는 온라인 저지 서비스입니다. 기업에서 선호하는 C, C++, 파이썬(Python), 자바(Java), 자바스크립트(Javascript) 이

level.goorm.io

3. 풀이

1. 2진수로 전환하는 과정이 필요하기에 전환을 한다

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
	
user_input = int(input())
bit2 = []
count = 0
num = 0 
bit16 = []

while user_input: ''' 없어질때까지 홀수면 1을 빼서 2로 나눠준다 아니면 그냥 나눈다'''
	if user_input % 2 == 1:
		user_input -= 1
		bit2.append(1)
	else:
		bit2.append(0)
	user_input /= 2

2. 16진수로 전환을 하자

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
def numbering16(number):
	numberdir = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
	if number >= 10 :
		return numberdir[number]
	else:
		return number
	
user_input = int(input())
bit2 = []
count = 0
num = 0 
bit16 = []
while user_input:
	if user_input % 2 == 1:
		user_input -= 1
		bit2.append(1)
	else:
		bit2.append(0)
	user_input /= 2

for i in bit2:   ''' 진수에 맞게 재곱하여 더하여 넣어준다 '''
	num += i*2**count ''' 2를 자리에 맞게 재곱하고 비트에 숫자가 있으면 1 이되어 합쳐지고 아니면 0 이되여 더할게없다'''
	
	if count == 3 :   ''' 2에 3승까지 가면 16진수니깐 값을 저장하고 0으로 되돌린다'''
		bit16.append(num)  
		count = 0
		num = 0
	else:
		count += 1
if count != 0:   '''  마즈막에 0 이 아닐경우 num 값이 입력되지않고 남아있기에 넣어주고 끝넨다'''
	bit16.append(num)  

3. 10이상의 숫자를 문자로 변환하고 출력하자

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean


def numbering16(number):        ''' 숫자를 문자로 전환해주는 딕션너리 함수를 만든다 '''
	numberdir = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
	if number >= 10 :   '''10 보다 클때만 적으면된다 '''
		return numberdir[number]
	else:
		return number
	
user_input = int(input())
bit2 = []
count = 0
num = 0 
bit16 = []
while user_input:
	if user_input % 2 == 1:
		user_input -= 1
		bit2.append(1)
	else:
		bit2.append(0)
	user_input /= 2

for i in bit2:
	num += i*2**count
	
	if count == 3 :
		bit16.append(num)
		count = 0
		num = 0
	else:
		count += 1
if count != 0:
	bit16.append(num)

for i in list(reversed(bit16)):   
	print(numbering16(i),end="") ''' 함수를 불러 형식에 맞게 출력한다'''

4. 결과

1. 위 풀이의 결과다

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
def numbering16(number):
	numberdir = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
	if number >= 10 :
		return numberdir[number]
	else:
		return number
	
user_input = int(input())
bit2 = []
count = 0
num = 0 
bit16 = []
while user_input:
	if user_input % 2 == 1:
		user_input -= 1
		bit2.append(1)
	else:
		bit2.append(0)
	user_input /= 2

for i in bit2:
	num += i*2**count
	
	if count == 3 :
		bit16.append(num)
		count = 0
		num = 0
	else:
		count += 1
if count != 0:
	bit16.append(num)

for i in list(reversed(bit16)):
	print(numbering16(i),end="")

2. hex 함수를 사용하여 편하게 작성 하는 방법이다 

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean

user_input = hex(int(input()))
print(user_input.replace("0x",""))

 

'Goorm ide 코딩 문제 > Level 1' 카테고리의 다른 글

[Goorm LEVEL 1] ASCII 코드  (0) 2021.04.13
[Goorm LEVEL 1] 몫과 나머지  (0) 2021.04.13
[Goorm LEVEL 1] 거스름돈  (0) 2021.04.13
[Goorm LEVEL 1] Bubble Sort  (0) 2021.04.12
[Goorm LEVEL 1] 삼각형의 넓이  (0) 2021.04.12
블로그 이미지

Or71nH

,