簡體   English   中英

如何創建數字列表

[英]How to create a list of numbers

請幫助我創建一個程序,該程序輸出可能的數字列表,其中包含N范圍內的所有數字(N是輸入數字),但不應有連續的數字

例:

N=4

n = 0,1,2,3的范圍n = 0,1,2,3

1032 False because (1 behind 0) and (3 behind 2) 

1230 False because (1 behind 2) and (2 behind 3)

2031 True because there no two consecutive numbers one behind the other
List=[1302,2031]

請檢查以下內容:

n = int(input())
l = list()
i = 0
for j in range(1,n+1):
   if j == 0:
      l.append(j)
   elif j % 2 == 0:
      l.insert(0, j)
   else:
      l.append(j)

print(l)

這實際上僅在“ n”為奇數時有效。 我希望你能弄清偶數情況。 如果沒有,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM