Day5習(xí)題回顧

  1. 讀程序
numbers=1
for i in range(0,20):
    numbers*=2
print(numbers)

i = 0, numbers = 12 = 2
i = 1, numbers = 1
22
i = 2, numbers = 1
222
...
i = 19, numbers = 2*20
print(2 *
20)

summation=0
num=1
while num<=100:
    if (num%3==0 or num%7==0) and num%21!=0:
        summation += 1
    num+=1
print(summation)

summation = 0
num = 1
num = 1 <= 100, if False, num += 1, num = 2
num = 2 <= 100, if False, num += 1, num = 3
num = 3 <= 100, if True, summation += 1, summation = 1, num += 1, num = 4
...
print(summation)

  1. 求1到100所有數(shù)的和、平均值
    for 循環(huán):
sum = 0
for x in range(101):
    sum += x
print('和:', sum, '平均值:', sum / 100)

sum = 0
x = 0, sum = 0 + 0 = 0
x = 1, sum = 0 +1 =1
x = 2, sum = 1 + 2 = 3
...
x = 100, sum = 0 + 1 + 2 +...+ 100
print(sum, sum / 100)

while循環(huán):

sum = 0
i = 1
while i <= 100:
    sum += i
    i += 1
print('和:', sum, '平均值:', sum / 100)

i = 1, i <= 100, sum = 0 + 1 = 1, i += 1, i = 2
i = 2, i <= 100, sum = 1 + 2 = 3, i += 1, i = 3
i = 3, i <= 100, sum = 1 + 2 + 3, i += 1, i = 4
...
i = 100, i <= 100, sum = 1+2+...+100, i += 1, i = 101
i = 101, while False, print(sum, sum / 100)

  1. 計(jì)算1-100之間能3整除的數(shù)的和
sum = 0
for x  in range(3, 100, 3):
    sum += x
print(sum)

sum = 0
x = 3, sum = 0 + 3
x = 6, sum = 0 + 3 + 6
...
x = 99, sum = 0 + 3 + 6 +...+99
print(sum)

sum = 0
for x in range(1, 100):
    if x % 3 != 0:
        continue
    sum += x
print(sum)

sum = 0
x =1 , if True, continue跳出當(dāng)前循環(huán)
x = 2, if True, continue跳出當(dāng)前循環(huán)
x = 3, if False, sum = 0 + 3
x = 4, if True, continue跳出當(dāng)前循環(huán)
...
x = 99, if False, sum = 0 + 3 + ....99
print(sum)

sum = 0
num = 3
while num <= 100:
    if num % 3 == 0:
        sum += num
    num += 1
print(sum)

sum = 0
num = 3, while True, if True, sum = 0 + 3, num = 4
num = 4, while True, if False, num = 5
...
num = 99, while True, if True, sum = 0 + 3 +... + 99, num = 100
num = 100, while True, if False, num = 101
num = 101, while False, print(sum)

  1. 求斐波那契數(shù)列列中第n個(gè)數(shù)的值:(0),1兔辅,1驮瞧,2氓扛,3,5论笔,8采郎,13,21狂魔,34....
    規(guī)則: 從第二個(gè)數(shù)開始蒜埋,后面的數(shù)的值就是這個(gè)數(shù)前兩個(gè)數(shù)的和
    for 循環(huán)
a = 0
b = 1
n = int(input('請(qǐng)輸入一個(gè)數(shù):'))
for x in range(1, n + 1):
    a, b = b, a + b
print(a)

a = 0 #定義當(dāng)前數(shù)的前兩個(gè)數(shù)
b = 1 # 定義當(dāng)前數(shù)的前一個(gè)數(shù)
n = int(input) # 從控制臺(tái)輸入要求的斐波那契數(shù)列中的第幾個(gè)數(shù)
x = 1, a = 1, b = 1
x = 2, a = 1, b = 2
x = 3, a = 2, b = 3
....

while 循環(huán):

a = 0
b = 1
c = 1
i = 1
n = int(input())
if n == 1:
    print(1)
While i < n:
    c = a + b
    a = b
    b = c
    i += 1
print(c)

a = 0 #定義當(dāng)前數(shù)的前兩個(gè)數(shù)
b = 1 # 定義當(dāng)前數(shù)的前一個(gè)數(shù)
c = 0 #定義一個(gè)當(dāng)前數(shù)字變量
i = 1 定義一個(gè)變量
n = int(input) # 從控制臺(tái)輸入要求的斐波那契數(shù)列中的第幾個(gè)數(shù) 暫時(shí)取5
if n == 1, 輸出1
if n != 1,執(zhí)行while循環(huán)最楷。
i = 1, while true, c = 0 +1 = 1, a = 1, b = 1, i =2
i = 2, while true, c= 1+ 1 = 2整份,a = 1, b = 2, i = 3
i = 3, while true, c= 1+ 2 = 3, a = 2, b = 3
i = 4, while true, c = 2 + 3 = 5, a = 3, b = 5, i = 5
i = 5, while False, print(c)

  1. 判斷101-200之間有多少個(gè)素?cái)?shù)待错,并輸出所有素?cái)?shù)。
    素?cái)?shù)(質(zhì)數(shù)): 除了1和它本身皂林,不能再被其他的數(shù)整除
count = 0
for x in range(101, 200):
    for i in range(2, x):
        if x % i == 0:
            break
    else:
        print(x)
        count += 1
print(count)

count = 0
x = 101,
i = 2, if False
i = 3, if False
...
i = 100, if False
print(x), count = 1
x = 102,
i = 2, if True, break
x = 103,
i = 2, if False
I = 3,if False
...
i = 102, if False
print(x), count = 2
...
x = 199,
i = 2, if False
i = 2, if False
...
i = 198, if False
print(x), count += 1

while循環(huán):

count = 0
num = 101
while num < 200:
    x = 2
    while x < num:
        if num % x == 0:
            break
        x += 1
    else:
        print(num)
        count += 1
    num += 1
print(count)

count = 0
num = 101, while True,
x = 2, x < 101, if False, x = 3
x = 3, x < 101, if False, x = 4
...
x = 100, x < 101, if False, x = 101
x = 101, while False, else, print(101),count = 1, num += 1
num = 102, while True,
x = 2, while True,if True, break, num += 1
...
num = 199, while True,
x = 2, x < 200, if False, x =3
x = 3, x < 200, if False, x =4
...
x = 198, x < num, if False, x = 199
x = 199, while False, print(199), count += 1 ,num += 1
num = 200, while False, print(count)
用標(biāo)志True結(jié)束循環(huán):

count = 0
for n in range(101, 200):
    is_prime = True
    for x in range(2, n):
        if n % x != 0:
            is_prime = False
            break
    if is_prime = True:
        print(n)
        count += 1
print(count)

count = 0
n = 101, is_prime = True,
x = 2, if False,
x = 3,if False
...
x = 100, if False
if True: print(102), count = 1
n = 102, is_prime = True, x = 2, if True, is_prime = False, break. if False
n = 199, is_prime = True,
x = 2, if False
x = 3, if False
...
x = 198, if False
if True: print(199),count += 1
print(count)

  1. 有一分?jǐn)?shù)序列:(1/1),2/1,3/2,5/3,8/5,13/8,21/13...求出這個(gè)數(shù)列的第20個(gè)分?jǐn)?shù)
    分子: 前一個(gè)分?jǐn)?shù)的分子+前一個(gè)分?jǐn)?shù)的分母
    分母: 前一個(gè)分?jǐn)?shù)的分子
    思路: 用兩個(gè)變量保存前一個(gè)分?jǐn)?shù)的分子和分母朗鸠, 然后根據(jù)規(guī)則不斷更新分子和分母的值
fen_zi = 1
fen_mu = 1
for x in range(20):
    fen_zi, fen_mu = fen_zi + fen_mu, fenzi
print(fen_zi + fen_mu, '/', fen_mu)

fen_zi = 1
fen_mu = 1
x = 1, fen_zi, fen_mu = 2, 1
x = 2, fen_zi, fen_mu = 3, 2
...
x= 20, fen_zi, fen_mu = fen_zi + fen_mu, fenzi
print(fen_zi + fen_mu, '/', fen_mu)

7.給一個(gè)正整數(shù),要求:1础倍、求它是幾位數(shù) 2.逆序打印出各位數(shù)字
解題思路: 看一個(gè)正整數(shù)是幾位數(shù)烛占,就看這個(gè)數(shù)整除幾次10以后會(huì)變成0

count = 0
n = int(input('請(qǐng)輸入一個(gè)數(shù)字: ')) #暫定為n = 12456
While True:
    print(n % 10)
    n //= 10
    count += 1
    if n == 0:
        break
print(count)

count = 0
print(6),n = 1245沟启,count = 1忆家,if False
print(5), n = 124, count = 2, if False
print(4), n = 12, count = 3, if False
print(2), n = 1, count= 4, if False
print(1), n =0, count = 5, if True, break,
print(count)

8.不斷的從控制臺(tái)輸入年齡,根據(jù)年齡輸出不同的提示(例如:老年人德迹,青壯年芽卿,成年人,未成年胳搞,兒童)
輸入'q'卸例,就退出

while True:
    value = input('請(qǐng)輸入你的年齡:')
    if value == 'q':
        break
    age = int(value)
    if age <= 12:
        print('兒童')
    elif 12 < age <= 18:
        print('未成年')
    elif 18 < age <= 50:
        print('青壯年')
    else:
        print('老年')  

進(jìn)入循環(huán):
請(qǐng)輸入年齡,如果輸入值='q',break肌毅,不等于'q'
將輸入的值轉(zhuǎn)為整型筷转;判斷age的范圍,輸出相應(yīng)的值

9.計(jì)算5的階乘 5!的結(jié)果

sum = 1
for x in range(1, 6):
    sum *= x
print(sum)

sum = 1
x = 1, sum = 1 * 1 = 1
x = 2,sum = 1 * 1 * 2
x = 3, sum = 1* 12 3
...
x = 5, sum = 1
2
345

10.求1+2!+3!+...+20!的和
思路: 1. 先分別取出1~20 2.求每次取出的數(shù)的階乘

sum1 = 0
for x in range(1, 21):
    sum2 = 1
    for i in range(1, x + 1):
        sum2 *= i
    sum1 += sum2
print(sum1)

sum1 = 0
x = 1, sum2 = 1. i = 1, sum1 = 1
x = 2, sum2 = 1,
i = 1, sum2 = 1
i = 2, sum2 = 12,
sum1 = 1! + 2!
x = 3, sum2= 1
i =1, sum2 = 1
i =2, sum2 = 1
2,
i =3, sum2 = 123,
sum1 = 1悬而!+ 2呜舒!+ 3!
...
x = 20, sum2 = 1
i =1, sum2 = 1
i =2, sum2 = 12,
i =3, sum2 = 1
23,
...
i = 20, sum = 1
2...*20
sum1 = 1!+ 2笨奠!+ 3!+...20!
print(sum1)

11.循環(huán)輸入大于0的數(shù)字進(jìn)行累加袭蝗,直到輸入的數(shù)字為0,就結(jié)束循環(huán)般婆,并最后輸出累加的結(jié)果

sum =0
while True:
    num = int(input('請(qǐng)輸入一個(gè)數(shù)字到腥,輸入0結(jié)束: '))
    if num == 0:
        break
    sum += num
print(sum)

sum = 0
進(jìn)入循環(huán),輸入數(shù)字蔚袍,如果num = 0, 跳出循環(huán)左电。如果不是0,sum = sum+num
再次進(jìn)入循環(huán)页响,直到輸入0跳出循環(huán)
print(sum)

12.求s=a+aa+aaa+aaaa+aa...a的值篓足,其中a是一個(gè)數(shù)字。例如2+22+222+2222+22222(此時(shí)共有5個(gè)數(shù)相加)闰蚕,幾個(gè)數(shù)相加有鍵盤控制栈拖。
解題思路:
2
22 = 210 + 2
222 = 22
10+2
2222= 2222*10 + 2

a = int(input('請(qǐng)輸入一個(gè)個(gè)位數(shù):')) #定義a的值 a = 2
n = int(input('請(qǐng)輸入一個(gè)數(shù):')) #決定多少位數(shù)相加,循環(huán)幾次 n = 5
i = 1
sum1 = 0 #保存最后求和的值
sum2 = 0 #保存每一項(xiàng)
while i <= n:
    sum2 = sum2*10 + a
    sum1 += sum2
    i += 1
print(sum1)

a = 2, n = 5, i = 1, sum1 = sum2 = 0
i = 1, while True, sum2 = 0 + 2 = 2, sum1 = 0 +2 = 2, i = 2
i = 2, while True, sum2 = 22, sum1 = 2 + 22, i = 3
i = 3, while True, sum2 = 222, sum1 = 2 + 22 + 222, i = 4
i = 4, while True, sum2 = 2222, sum1 = 2 + 22 + 222 + 2222, i = 5
i = 5, while True, sum2 = 22222, sum1 = 2 + 22 + 222 + 2222 + 22222, i = 6
print(sum1)

13.由小到大輸出x, y, z 中最大的數(shù)
解題思路:先假設(shè)最小的數(shù)為x, 再用x 與其他數(shù)字比較没陡,如果其他數(shù)字比x小涩哟, 則將較小的值賦給min

x = int(input('enter your first number: ')) 3
y = int(input('enter your first number: '))5
z = int(input('enter your first number: '))1
if x > y:
    x, y = y, x
if x > z:
    x, z = z, x
if y > z:
    y, z = z, y
print(x, y, z)

if False, if 3>1, x = 1, z = 3
if 5>3, y = 3, z = 5

  1. 控制臺(tái)輸出三角形
    根據(jù)n的值的不同索赏,輸出相應(yīng)的形狀
    ''''



**
*

n = 5
for x in range(5,0, -1):
    for i in range(x):
        print('*', end = '')
    print()

n = 5
x = 5, i = 0, *
i = 1, **
...
i = 4, *****
x = 4, i = 0, *
i = 1, **
...
i = 3, ****
...
x = 1, i = 0, *

根據(jù)n的值的不同,輸出相應(yīng)的形狀(n為奇數(shù))

n = 7
for x in range(1, n + 1, 2):
    space = int(n - star) / 2
    for _ in range(space):
        print(' ', end = '')
    for _ in range(star):
        print('*', end = '')
    print()

x = 1, space = 3
*
x = 3, space = 2
***
...
x = 7, space = 0
*******

  1. 這是經(jīng)典的"百馬百擔(dān)"問題贴彼,有一百匹馬潜腻,馱一百擔(dān)貨,大馬馱3擔(dān)器仗,中馬馱2擔(dān)融涣,兩只小馬馱1擔(dān),問有大精钮,中威鹿,小馬各幾匹?
for small in range(1, 100):
    for middle in range(1, 50):
        for big in range(1, 33):
            if (big + middle + small == 100 )and  (big * 3 + middle * 2 + small / 2 == 100):
            print('大:', big, '中:', middle, '小:', small)

small = 1, middle = 1, big = 1, if False
small = 1, middle = 1, big = 2, if False
...

16.輸出9*9口訣轨香。 1.程序分析:分行與列考慮忽你,共9行9列,i控制行臂容,j控制列科雳。
左下角輸出

for i in range(1, 10):
    for j in range(1, i + 1):
        print(j, ' x ', i, '= ',i*j, end = ' ')
    print()

i = 1,j = 1, print(1 * 1 = 1)
i = 2,
j = 1, print(1 * 2 = 2)
j = 2, print(2 * 2 = 4)
i = 3,
j = 1, print(1 * 3 = 3)
j = 2, print(2 * 3 = 6 )
j = 3, print(3 * 3 = 9)

...
i = 9,
j = 1, print(1 * 9 = 9)
j = 2, print(2 * 9 = 19)
j = 3, print(3 * 9 = 27)
...
j = 9, print(9 *9 = 81)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末脓杉,一起剝皮案震驚了整個(gè)濱河市糟秘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌丽已,老刑警劉巖蚌堵,帶你破解...
    沈念sama閱讀 212,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件买决,死亡現(xiàn)場(chǎng)離奇詭異沛婴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)督赤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,755評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門嘁灯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人躲舌,你說我怎么就攤上這事丑婿。” “怎么了没卸?”我有些...
    開封第一講書人閱讀 158,369評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵羹奉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我约计,道長(zhǎng)诀拭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,799評(píng)論 1 285
  • 正文 為了忘掉前任煤蚌,我火速辦了婚禮耕挨,結(jié)果婚禮上细卧,老公的妹妹穿的比我還像新娘。我一直安慰自己筒占,他們只是感情好贪庙,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,910評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著翰苫,像睡著了一般止邮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上革骨,一...
    開封第一講書人閱讀 50,096評(píng)論 1 291
  • 那天农尖,我揣著相機(jī)與錄音,去河邊找鬼良哲。 笑死盛卡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的筑凫。 我是一名探鬼主播滑沧,決...
    沈念sama閱讀 39,159評(píng)論 3 411
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼巍实!你這毒婦竟也來了滓技?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,917評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤棚潦,失蹤者是張志新(化名)和其女友劉穎令漂,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丸边,經(jīng)...
    沈念sama閱讀 44,360評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡叠必,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,673評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了妹窖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纬朝。...
    茶點(diǎn)故事閱讀 38,814評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖骄呼,靈堂內(nèi)的尸體忽然破棺而出共苛,到底是詐尸還是另有隱情,我是刑警寧澤蜓萄,帶...
    沈念sama閱讀 34,509評(píng)論 4 334
  • 正文 年R本政府宣布隅茎,位于F島的核電站,受9級(jí)特大地震影響嫉沽,放射性物質(zhì)發(fā)生泄漏辟犀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,156評(píng)論 3 317
  • 文/蒙蒙 一耻蛇、第九天 我趴在偏房一處隱蔽的房頂上張望踪蹬。 院中可真熱鬧胞此,春花似錦、人聲如沸跃捣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)疚漆。三九已至酣胀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間娶聘,已是汗流浹背闻镶。 一陣腳步聲響...
    開封第一講書人閱讀 32,123評(píng)論 1 267
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丸升,地道東北人铆农。 一個(gè)月前我還...
    沈念sama閱讀 46,641評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像狡耻,于是被迫代替她去往敵國(guó)和親墩剖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,728評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • 在C語言中,五種基本數(shù)據(jù)類型存儲(chǔ)空間長(zhǎng)度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,339評(píng)論 0 2
  • 寫在前面的話 代碼中的# > 表示的是輸出結(jié)果 輸入 使用input()函數(shù) 用法 注意input函數(shù)輸出的均是字...
    FlyingLittlePG閱讀 2,744評(píng)論 0 8
  • 【程序1】 題目:古典問題:有一對(duì)兔子夷狰,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子岭皂,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    開心的鑼鼓閱讀 3,311評(píng)論 0 9
  • 01-常量與變量 學(xué)習(xí)swift第一步打印Hello World print("Hello World") swi...
    iOS_恒仔閱讀 5,134評(píng)論 2 19
  • 8月22日-----字符串相關(guān) 2-3 個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息沼头。顯示的消...
    future_d180閱讀 964評(píng)論 0 1