循环判断练习
循环判断练习
1、使用for和while循环输出 0~100内所有3的倍数。
for i in range(101):
if i % 3 == 0:
print(i,end=' ')
print()
num = 0
while num < 100:
if num % 3 == 0:
print(num,end=" ")
num +=1
2、使用for和while循环输出100~200内个位数或者十位数能被3整除的数。
for i in range(100, 201):
gewei = i % 10
shiwei = i // 10 % 10
if (gewei % 3 == 0 and gewei != 0) or (shiwei % 3 == 0 and shiwei !=0 ):
print(i, end=" ")
print()
num = 100
while num <= 200:
gewei = num % 10
shiwei = num // 10 % 10
if (gewei % 3 == 0 and gewei != 0) or (shiwei % 3 == 0 and shiwei != 0):
print(num,end=" ")
num +=1
3、打印三角形
x = 0
while x < 5:
i = 0
while i <= x:
print("@", end=' ')
i += 1
print()
x += 1
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
4、打印九九乘法表
m = 1
while m <= 9: # 控制列
n=1
while n <=m:
print(n, " * ", m, '=' ,m*n,end=' ')
n +=1
m +=1
print()
5、反向打印九九乘法表
m =9
while m >=0:
n = 1
while n <=m:
print(n, "*", m, '=' , m*n ,end=" ")
n +=1
# print('*',end=" ")
m -=1
print()
6、统计100~200中十位数是5的数的个数
count = 0
for i in range(100,200):
if i // 10 % 10 == 5:
print(i, end=" ")
count +=1
print()
print(count)
i = 100
con = 0
while i <=200:
if i // 10 % 10 == 5:
print(i, end=" ")
con +=1
i += 1
print()
print(con)
7、使用循环计算1*2*3*4*...*10
的结果
j = 1
for i in range(1,11):
j *=i
print(j)
print(j)
count =1
num=1
while count <=10:
num *=count
count +=1
print(num)
8、统计100以内个位数是2并且能够被3整除的数的个数。
for i in range(100):
if i % 10 == 2 and i % 3 == 0:
print(i,end=" ")
print()
num=0
while num < 100:
if num % 10 == 2 and num % 3 == 0:
print(num,end=' ')
num +=1
9、输入任意一个正整数,求他是几位数?
x = int(input("请输入一个正整数:"))
count = 0
print("---",len(str(x)),"---")
while x != 0:
x //= 10
count +=1
print(count)
10、打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字⽴方和等于该数本身。例如:153是。
for i in range(100, 1000):
g = i % 10
s = i // 10 % 10
b = i // 100
if g ** 3 + s ** 3 + b ** 3 == i:
print(i)
print('-------')
num = 100
while num < 1000:
g = num % 10
s = num // 10 % 10
b = num // 100
if g ** 3 + s ** 3 + b ** 3 == num:
print(num)
num +=1
11、判断指定的数是否是素数(素数就是质数,即除了1和它本身以外不能被其他的数整除的数)
num = int(input("请输入一个数字:"))
for i in range(2,num):
if num % i == 0:
break
else:
print(num,"是质数")
11、求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34… (这儿的n可以是任意正整数,可以通过输入来确定)
num1=1
num2=1
n = int(input("请输入第几位数字:"))
for i in range(0,n-2):
x= num1
num1=num2
num2=x+num2
12、这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?(可以直接使用穷举法)
13、一张纸0.08mm,对折多少次后有高度8848.13m.
height = 0.08/1000
count = 0
while True:
height *=2
count +=1
print(height)
if height >= 8848.13:
break
print(count)