常州百度搜索優(yōu)化seo刷詞工具在線
目錄
1、用循環(huán)語句求1+22+333+4444+55555的和
?2、求出2000-2100的所有閏年,條件是四年一閏,百年不閏,四百年再閏
3、輸入兩個正整數(shù),并求出它們的最大公約數(shù)和最小公倍數(shù)
4、求出100以內(nèi)的所有質(zhì)數(shù)
5、求100以內(nèi)最大的10個質(zhì)數(shù)的和
6、求1到10所有的偶數(shù)和
7、將10-20不能被2或3整除的數(shù)輸出
1、用循環(huán)語句求1+22+333+4444+55555的和
for i in range(1,6):a = str(i)lst.append(a)
for j in range(0,5):b = int(lst[j] * (j + 1))sum += b
print("sum = ",sum)
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\04.py
sum = 60355Process finished with exit code 0
?2、求出2000-2100的所有閏年,條件是四年一閏,百年不閏,四百年再閏
lst = []
for i in range(2000,2101):if i%4 == 0 and i%100 != 0 or i%400 == 0:lst.append(i)
print(lst)
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\05.py
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096]Process finished with exit code 0
3、輸入兩個正整數(shù),并求出它們的最大公約數(shù)和最小公倍數(shù)
a = int(input("請輸入第一個整數(shù):"))
b = int(input("請輸入第二個整數(shù):"))
if a > b:a , b = b , a
for i in range(a,0,-1):if a % i == 0 and b % i == 0:print("%d和%d的最大公約數(shù)是%d"%(a , b , i))print("%d和%d的最小公倍數(shù)是%d"%(a , b , a * b / i))break
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\06.py
請輸入第一個整數(shù):20
請輸入第二個整數(shù):26
20和26的最大公約數(shù)是2
20和26的最小公倍數(shù)是260Process finished with exit code 0
4、求出100以內(nèi)的所有質(zhì)數(shù)
lst = []
flag = True
for i in range(2,101):for j in range(2,i):if i % j == 0:flag = Falsebreakelse:flag = Trueif flag == True:lst.append(i)
print(lst)
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\07.py
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]Process finished with exit code 0
5、求100以內(nèi)最大的10個質(zhì)數(shù)的和
flag =True
sum = 0
cnt = 0
for i in range(100,1,-1):for j in range(2,i):if i % j == 0:flag = Falsebreakelse:flag = Trueif flag == True:sum += icnt += 1if cnt == 10:print("100以內(nèi)最大的10個質(zhì)數(shù)的和為:",sum)break
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\08.py
100以內(nèi)最大的10個質(zhì)數(shù)的和為: 732Process finished with exit code 0
6、求1到10所有的偶數(shù)和
sum = 0
for i in range(1,11):if i % 2 == 0:sum += i
print("1到10所有的偶數(shù)和為:",sum)
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\09.py
1到10所有的偶數(shù)和為: 30Process finished with exit code 0
7、將10-20不能被2或3整除的數(shù)輸出
for i in range(10,21):if i % 2 != 0 and i % 3 != 0:print(i)
運行結(jié)果:
F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\10.py
11
13
17
19Process finished with exit code 0