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

Blue Bridge Cup exercise system 01 string (Python)

編輯:Python

Problem description

For length is 5 One of the bits 01 strand , Everyone can be 0 or 1, Altogether 32 Maybe . The first few of them are :
00000
00001
00010
00011
00100
Please output this in order of small to large 32 Kind of 01 strand .

Output format

Output 32 That's ok , The length of each line in the order from small to large is 5 Of 01 strand .

Reference code

# Method 1 :
for i in range(0,32):
print("{0:0>5b}".format(i))
# Method 2 :
for a in range(0, 2):
for b in range(0, 2):
for c in range(0, 2):
for d in range(0, 2):
for e in range(0, 2):
print(str(a) + str(b) + str(c) + str(d) + str(e))

Investigate knowledge points

1. The length is 5 Of 01 String , The maximum value is 11111, convert to 10 Hexadecimal is 31, The idea of method 1 is to start with 0~32 Search once in this range , And then convert the retrieved number into binary number for output
2.format() Function by {} and : To replace the old %
The number before the colon is actually the serial number , When format When only one element needs to be output , This serial number can be omitted ( A colon cannot be omitted )
format Use >、、< To indicate left alignment 、 Align center 、 Right alignment . Align symbols in (>、、<) The number added after , Width , Adding a specific character before the alignment symbol indicates that when the output is not enough to take up the full width , Fill with specific characters .
This question is to use 0 placeholder , Right alignment , Width is 5
About formate For detailed usage of the function, please refer to the following article
Python Format output format function
2. The second way is through 5 Layer cycle + Character splicing to achieve output


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