程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Blue Bridge Cup - hex to octal -python | CSDN creation punch in

編輯:Python

List of articles

    • Problem description
    • Input format
    • Output format
    • Examples Input
    • Sample output
    • Implementation code
    • Investigation contents

Problem description

Given n Six positive hexadecimal integers , Output their corresponding octal numbers .

Input format

The first line of input is a positive integer n (1<=n<=10).
Next n That's ok , Each line is made up of 09、 Capital AF Composed string , Represents the hexadecimal positive integer to convert , The length of each hexadecimal number does not exceed 100000.

Output format

Output n That's ok , Enter the corresponding octal positive integer for each line .
【 Be careful 】
The hexadecimal number you enter will not have a leader 0, such as 012A.
The output octal number also can't have leading 0.
【 Tips 】
First, convert a hexadecimal number to a hexadecimal number , And then from a base number to octal .

Examples Input

 2
39
123ABC

Sample output

 71
4435274

Implementation code

n=input()
x=0
shuju=[]
for i in range(int(n)):
m=input()
shuju.append(m)
for i in range(int(n)):
k = oct(int(shuju[i], 16))# Binary conversion 
print(k[2:])# Rounding off 

Investigation contents

1. Hexadecimal conversion
All binary conversions need to be done with int( Number of converted , Original data base type ) Change to decimal for final decimal conversion

 2 Hexadecimal to octal oct(int(n,2)) First convert binary to decimal and then convert decimal to octal
Binary system bin()
Decimal system int()
octal oct()
Hexadecimal hex()

2. Remove the hexadecimal symbol at the time of output

 Assume the data value is n
Use n[2:] Discard the first two digits of the list

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved