日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎么修改while循环类型_python 的for与while 的i改变

發布時間:2024/10/8 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么修改while循环类型_python 的for与while 的i改变 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近使用實驗樓擼代碼 http://www.shiyanlou.com/register?inviter=NTY0MzE5NDE2NjM5

做一道count and say 的算法題的時候,有c++語法的解題答案,我改成用python

題目:

The count-and-say sequence is the sequence of integers beginning as follows:

1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.

11 is read off as "two 1s" or 21.

21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Example

Given n = 5, return "111221".

Note

The sequence of integers will be represented as a string.

在用python寫的時候 我用了for中有while循環,想在while中改變i的值,也能影響外層的for中的i,但是我發現改變里層的i與外層for的i沒有影響,導致程序出錯

這是c++解法:

string countAndSay(int n) {

if (n == 0) return "";

string res = "1";

while (--n) {

string cur = "";

for (int i = 0; i < res.size(); i++) {

int count = 1;

while ((i + 1 < res.size()) && (res[i] == res[i + 1])){

count++;

i++;

}

cur += to_string(count) + res[i];

}

res = cur;

}

return res;

}

python也使用for和while時,內層的while修改了i值并不會傳遞到外層, 使重復數據被加入結果集

class Solution:

def countAndSay(self,n):

if n==0 :

return ""

res="1"

n=n-1

while n:

print ('n:',n)

print('res length',len(res))

cur=""

i=0

for i in range(len(res)):

count=1

print 'i in for:',i

while((i+1

print "i in while,before +1",i,'count',count

count=count+1

i=i+1

print 'i in while:',i,'count:',count

cur =cur+ str(count)+res[i]

print ('cur:',cur)

res=cur

print 'res:',res

n=n-1

return res

aa=Solution()

print (aa.countAndSay(4))

結果是:

shiyanlou:Code/ $ python countAndSay.py [12:35:02]

('n:', 3)

('res length', 1)

i in for: 0

('cur:', '11')

res: 11

('n:', 2)

('res length', 2)

i in for: 0

i in while,before +1 0 count 1

i in while: 1 count: 2

('cur:', '21')

i in for: 1

('cur:', '2111')

res: 2111

('n:', 1)

('res length', 4)

i in for: 0

('cur:', '12')

i in for: 1

i in while,before +1 1 count 1

i in while: 2 count: 2

i in while,before +1 2 count 2

i in while: 3 count: 3

('cur:', '1231')

i in for: 2

i in while,before +1 2 count 1

i in while: 3 count: 2

('cur:', '123121')

i in for: 3

('cur:', '12312111')

res: 12312111

12312111

python 改成while嵌套while時,程序就沒有問題了,因為內層修改的i會傳遞到上一層

class Solution:

def countAndSay(self,n):

if n==0 :

return ""

res="1"

n=n-1

while n:

print ('n:',n)

print('res length',len(res))

cur=""

i=0

while i < len(res):

count=1

print 'i in for:',i

while((i+1

print "i in while,before +1",i,'count',count

count=count+1

i=i+1

print 'i in while:',i,'count:',count

cur =cur+ str(count)+res[i]

print ('cur:',cur)

i=i+1

res=cur

print 'res:',res

n=n-1

return res

aa=Solution()

print (aa.countAndSay(4))

結果是:

shiyanlou:Code/ $ python countAndSay.py [11:13:53]

('n:', 3)

('res length', 1)

i in for: 0

('cur:', '11')

res: 11

('n:', 2)

('res length', 2)

i in for: 0

i in while,before +1 0 count 1

i in while: 1 count: 2

('cur:', '21')

res: 21

('n:', 1)

('res length', 2)

i in for: 0

('cur:', '12')

i in for: 1

('cur:', '1211')

res: 1211

1211

總結

以上是生活随笔為你收集整理的python怎么修改while循环类型_python 的for与while 的i改变的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久久久亚洲av无码麻豆 | 黄色网址在线免费看 | 日本毛片在线看 | 好色艳妇小说 | 国产一区二区三区免费观看视频 | 精品在线视频免费观看 | 中日韩欧美在线观看 | 国产麻豆一区二区三区在线观看 | 免费一二三区 | 好姑娘在线观看高清完整版电影 | www.成人网| 99产精品成人啪免费网站 | 91桃色在线观看 | 婷婷爱五月 | 国产欧美视频一区 | 国产精品xxxxx | 亚洲欧美日韩图片 | 啊灬啊灬啊灬秀婷 | 久久久久久激情 | 美女久久 | 高跟鞋和丝袜猛烈xxxxxx | 不卡视频在线观看免费 | 欧美日韩中文在线观看 | 亚洲天堂毛片 | 免费污网站在线观看 | 成人综合久久 | 视频一区二区三区四区五区 | 日韩精品欧美在线 | 久久久一本 | 亚洲精品自拍偷拍 | 91av看片 | 免费a网 | 久久久久久久久成人 | 日韩视频免费观看高清完整版在线观看 | 亚洲精品久久久久久动漫器材一区 | 狠狠鲁影院 | 国产精品丝袜视频无码一区69 | 日本成人免费在线 | www.av88| 久久亚洲一区二区 | 精品一区二区三区毛片 | 日韩中文字幕2019 | 久久久精品国产sm调教网站 | 九九精品在线播放 | 精品视频一区在线观看 | 偷偷在线观看免费高清av | 女儿的朋友5中汉字晋通话 欧美成人免费高清视频 | 欧美亚洲色图视频 | 欧美日韩免费高清一区色橹橹 | 91直接进入 | 老司机狠狠爱 | 呦呦在线视频 | 中文字幕一区二区人妻在线不卡 | 喷水在线观看 | 国产成人久久婷婷精品流白浆 | 国产成人77亚洲精品www | 综合婷婷 | 成人中文网 | 岛国av动作片 | a级特黄视频 | 波多野结衣一区二区三区高清av | 97视频在线观看免费高清完整版在线观看 | 裸体av淫导航 | 婷婷综合av | 久久综合色网 | 人妻夜夜爽天天爽 | 亚洲精品中文在线 | 涩涩涩在线观看 | 特级少妇 | 911精品 | 久久精品国产亚洲av蜜臀色欲 | 91视频成人 | 亚州春色| 日批视频免费播放 | 亚洲专区欧美专区 | 欧美久久久精品 | 樱花电影最新免费观看国语版 | 久久久久99精品成人片我成大片 | 成人精品视频 | 日韩不卡视频在线观看 | 国产高清一| 亚洲嫩草影院 | 无码少妇一区二区 | 日本黄色中文字幕 | av动漫免费观看 | 色亚洲视频 | 一区二区三区四区五区在线视频 | 粗大的内捧猛烈进出在线视频 | 欧美男人的天堂 | 色网址在线 | 欧美爽妇 | www.日| 欧美色视频一区二区三区 | jjzz日本女人| 女人性做爰24姿势视频 | 全部毛片永久免费看 | 天天撸在线视频 | 亚洲天堂日本 | 成人毛片18女人 |