1樓:
from
random
import
choices
ilist
=choices
(list
(range(1
,11)),k
=200000
)def
fuc(
ilist):d
=[1]
z=1foriin
range(1
,len
(ilist
)):if
ilist[i
-1]+
1==ilist[i
]:z+=1
d.(z
)else:d
.(z:
=1)returnd.
index
(maxint:=
max(
d)),
maxint
fuc(
ilist)
2樓:懷莜
from
random
import
randint
string0
='''
try:
next(it1)
while 1:
if next(it1) == next(it2){}1:
count += 1
else:
count = 1
except StopIteration:
pass'''
defconnected(ls
):"""計算最大的連續數值."""
print(ls
)ls.(
0)counts=
exec
(string0
.format
('+'),)
exec
(string0
.format
('-'),)
(max
(counts
))connected
([randint(1
,10)foriin
range(20
)])不請自來。以上僅供參考。之後可能會試著更一些更簡單的方法。
嗯,其實我也是個萌新(大佬太多了,我閃),還有一些問題沒得到解決,比如那兩行exec中的globals變數。那個字典變數不能事先賦值然後直接引用,使用copy方法建立副本也不行。否則,只會執行上面一行語句。
當然,如果題主所指的連續數字僅僅指的是遞增連續的話,那問題不大。
可能會感覺這個方法很奇怪,這個是來自於初高中學的vb中for...next這個結構,然後發現py中竟然也有next這個函式(雖然不是關鍵字……)
這道題怎麼用Python解?
嗯嗯 乙個約瑟夫環問題,先給這n個人編號,存在列表中,報數不報到k的人移到列表尾部,報到k的人就彈出列表 defjosephus population,k person list range 1 population 1 number 1 while len person 1 ifnumber k ...
這個程式用python怎麼寫呀?
封掣 s input input a string count s count a res s replace a print res print count 意群 不知道樓上為啥寫那麼複雜。defmycount string count 0 fors instring if s a count 1...
這個python實驗報告題怎麼做
冰糖雪梨 我本人不是專業的,給乙個實現方式,如下。如果有錯誤,還請各位批評指正。1 usr bin env python323 List creation 4 circle list chr i for i in range 65,65 26 56 number 1 7 time 0 8 while...