当前位置:网站首页>Blue Bridge Cup real question: one question with clear code, master three codes

Blue Bridge Cup real question: one question with clear code, master three codes

2022-07-06 18:19:00 Py Xiaozheng

Distance Blue Bridge Cup 56 God  

The purpose of learning algorithm is to improve yourself  

Thank a station for explaining the three codes Portal Original code Inverse code Complement code _ Bili, Bili _bilibili

  Topic link

  Problem analysis : Many friends must be like Xiao Zheng Yes Explanation of the title stem : give Bytes as the value of a signed integer Don't understand at the beginning Why there are negative numbers ? For Xiaobai like Xiao Zheng , One byte, eight bits , The range of data represented [00000000,11111111] namely [0,255] Where did the negative number come from ? 

  Actually The above understanding is based on Unsigned data , actually , There is another kind of numerical data called Signed data .

There are three representations of signed data : Original code , Inverse code , Complement code

As long as you know the original code   The latter two codes can be easily solved

8 Bit source code highest 0 A positive sign ,1 A minus sign The range of the last seven digits is [0,127]

therefore 8 Range of bit source code [-127,127] So there are two steps to determine the original code of a number :1: The sign determines the highest position 2: The absolute value of the number determines the last seven digits

Understand the original code The inverse code is easy to understand : The inverse code is based on the original code , Except for the highest position ( Sign bit ), The rest is reversed

Understand the inverse code Complement is easy to understand : Complement is based on inverse On the basis of irony +1

And these three codes , Complement is the most important Because in the computer system , All values are represented and stored by complements .

So the following focuses on the method of calculating complement ( This question is also based on complement )

First study negative numbers :

for instance : seek -1 Complement So we know -1 The original code of is 10000001

that -1 The inverse of is 11111110 that -1 The complement of is 11111111

It's that simple . Then for negative numbers [-127,-1] Can follow the above similar requirements -1 The method of calculation

And then to -128, We Regulations Its complement is 10000000

Then study positive numbers :

A complement to a positive number = The inverse of a positive number = The original code of a positive number     Sum up 8 The representation range of bit complement [-128,127]

therefore , After knowing the three codes ( The most important thing is complement ), Return to the topic , Right now Bytes as the value of a signed integer Is the concept of clear ? In fact, let's find the complement of each integer  

Each line of Chinese characters has 32 Bytes make up ,16*16 The pixel : a line 2 Bytes , A byte 8 position , form 16 That's ok

So print it out : So the question is what is the power of nine ?

s="""4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16 
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4 
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64 
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128 
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0 
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0 
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0 
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0"""
a=s.split('\n')

dict={}

def reverse(str):
    ans=''
    for i in str:
        ans+='0' if int(i)==1 else '1'
    return ans

for j in range(-128,128):
    if j>=0:
        dict[j]=(8-len(bin(j)[2:]))*'0'+bin(j)[2:]
    elif j==-128:
        dict[j]='10000000'
    else:
        tmp=reverse((7-len(bin(abs(j))[2:]))*'0'+bin(abs(j))[2:])# Except for sign bit negation 
        tmp_add=bin(int(tmp,2)+1)[2:]#+1
        dict[j]='1'+(7-len(tmp_add))*'0'+tmp_add
    
for i in a:
    tmp=list(map(int,i.split()))
    start=0#end=31
    while start<=30:
        s=dict[tmp[start]]+dict[tmp[start+1]]
        print(s)
        start+=2
    print('\n')
    

Because the length is too long , Just put one ‘ Nine ’ Okay , Specifically, you can run the code by yourself

                         ​​​​​​​        ​​​​​​​ I'm Xiao Zheng Is running to love Go to the mountains and seas  

原网站

版权声明
本文为[Py Xiaozheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131301007896.html