当前位置:网站首页>Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
2022-04-23 10:34:00 【Ape knowledge】
Series index : Writing diary of vegetables | By LeetCode use Python The days of abuse
Never thought of it , A few days after returning home for the Chinese New Year break , I will do some questions on New Year's Eve because of boredom , Then write an explanation .
List of articles
【 subject 】
Topic link :http://lx.lanqiao.cn/problem.page?gpid=T51
【 My code 】
My idea is : First Hexadecimal
turn Binary system
, Then turn octal
, Here, two dictionaries are directly created to store data . The code needs to pay attention to that the binary matches upside down , And when there are less than three numbers, you need to make up 0, Everyone else should be able to see .
book1 = {
'0': '0000',
'1': '0001',
'2': '0010',
'3': '0011',
'4': '0100',
'5': '0101',
'6': '0110',
'7': '0111',
'8': '1000',
'9': '1001',
'A': '1010',
'B': '1011',
'C': '1100',
'D': '1101',
'E': '1110',
'F': '1111'
}
book2 = {
'000': '0',
'001': '1',
'010': '2',
'011': '3',
'100': '4',
'101': '5',
'110': '6',
'111': '7'
}
n = int(input())
sn = ['' for _ in range(n)]
for i in range(n):
sn[i] = input()
for s in sn: # Get binary form number
num = ''
for j in s:
num += book1[j]
ss = ''
t = ''
count = 0
for k in num[::-1]:
t = k+t
count += 1
if count == 3: # Count in groups of three , When three are converted later
count = 0
ss = book2[t] + ss
t = ''
if count == 1:# There are two situations when there are less than three , Fill in the front respectively 0, Be careful not to write +=
t = '00'+t
ss = book2[t] + ss
if count == 2:
t = '0' + t
ss = book2[t]+ss
print(int(ss))
【 Binary conversion notes 】
2 Base number | 8 Base number | 10 Base number | 16 Base number | |
---|---|---|---|---|
2 Base number | — | bin(int(n,8)) | bin(int(n,10)) | bin(int(n,16)) |
8 Base number | oct(int(n,2)) | — | oct(int(n,10)) | oct(int(n,16)) |
10 Base number | int(n,2) | int(n,8) | — | int(n,16) |
16 Base number | hex(int(n,2)) | hex(int(n,8)) | hex(int(n,10)) | — |
Use Python Built in function for binary conversion , It's not hard to find that they are all First convert to decimal Then convert it to the corresponding hexadecimal , Let's give a simple example , Others will not be repeated :
# 8 Turn into the system 16 Base number :8->10->16
n=input()
print(hex(int(n,8)))
Input :1010
Output :0x208
If you don't want to take the first two marks , The following methods can be used :
- Method 1 : section , Take the result from the third character
# 10 Base to zero 2 Base number
n=int(input())
print(bin(n)[2:]) # Slicing operation
Input :10
Output :12
- Method 2 : Use format Function to format ,format Function has its own conversion function ,yyds
n=input()
print("{:b}".format(int(n,8)))
# First the 8 The base number is converted to 10 Base number ,
# And then in format Add a... To the slot of b, Equivalent to realizing bin Function functions
# But the result is without 0b Prefixed
Input :1010
Output :1000001000
Other bases :(n On the right is the original base )
print("{:o}".format(int(n,16))) # Convert other types to 8 Base number
print("{:x}".format(int(n,8))) # Convert other types to 16 Base number
【 reflection 】
Happy New Year! !
Python The series of force buckle problem solving is continuously updated , welcome
Like collection
+Focus on
Last one : Writing diary of vegetables | 20. Valid parenthesis (B Standing pen test questions )
Next : Writing diary of vegetables | Blue Bridge Cup — Substring score 【 The 11th 】【 Provincial competition 】【A Group 】
My level is limited , Please comment and correct the deficiencies in the article in the comment area below ~If feelings help you , Point a praise Give me a hand ~
Share... From time to time Interesting 、 Have a material 、 Nutritious content , welcome Subscribe to follow My blog , Looking forward to meeting you here ~
版权声明
本文为[Ape knowledge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230619309967.html
边栏推荐
- 707. Design linked list (linked list)
- JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
- Read integrity monitoring techniques for vision navigation systems
- 206、反转链表(链表)
- C语言——自定义类型
- 24、两两交换链表中的节点(链表)
- 基于PyQt5实现弹出任务进度条功能示例
- 59. Spiral matrix (array)
- 中职网络安全2022国赛之CVE-2019-0708漏洞利用
- Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
猜你喜欢
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
Solve the problem of installing VMware after uninstalling
解决方案架构师的小锦囊 - 架构图的 5 种类型
MapReduce compression
Zhengda international explains what the Dow Jones industrial index is?
Sim Api User Guide(6)
Reading integrity monitoring techniques for vision navigation systems - 3 background
C language - custom type
lnmp的配置
随机推荐
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
1. Sum of two numbers (hash table)
解决方案架构师的小锦囊 - 架构图的 5 种类型
一文看懂 LSTM(Long Short-Term Memory)
2022 mobile crane driver test question bank simulation test platform operation
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
Sim Api User Guide(4)
Sim Api User Guide(6)
MySQL common statements
【leetcode】107. Sequence traversal of binary tree II
任意文件读取漏洞 利用指南
Sim Api User Guide(7)
59、螺旋矩阵(数组)
Chapter 120 SQL function round
MySql常用语句
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
/etc/shadow可以破解吗?
C#和数据库连接中类的问题
Sim Api User Guide(5)