# 方法一:while循环
i = 1
while i <= 10:
j = 1
while j <= 10:
print("*",end="")
j += 1
i += 1
print()
# 方法二:for循环
for i in range(1,11):
for j in range(1,11):
print("*",end="")
print()
# 方法一:while循环
i = 1
while i <= 10:
j = 1
while j <= 10:
if j % 2 == 1:
print("★",end="")
else:
print("☆",end="")
j += 1
i += 1
print()
# 方法二:for循环
for i in range(1,11):
for j in range(1,11):
if j % 2 == 1:
print("★",end="")
else:
print("☆",end="")
print()
# 方法一:while循环
i = 1
while i <= 10:
j = 1
while j <= 10:
if i % 2 == 1:
print("★",end="")
else:
print("☆",end="")
j += 1
i += 1
print()
# 方法二:for循环
for i in range(1,11):
for j in range(1,11):
if i % 2 == 1:
print("★",end="")
else:
print("☆",end="")
j += 1
i += 1
print()
# 方法一:while循环
i = 1
while i <= 9:
j = 1
while j <= i:
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
j += 1
i += 1
print()
# 方法二:for循环
for i in range(1,10):
for j in range(1,i+1):
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
print()
# 方法一:while循环
i = 9
while i >= 1:
j = 1
while j <= i:
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
j += 1
i -= 1
print()
# 方法二:for循环
for i in range(9,0,-1):
for j in range(1,i+1):
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
print()
# 方法一:while循环
i = 1
while i <= 9:
k = 9 - i
while k > 0:
print(7*" ",end="")
k -= 1
j = 1
while j <= i:
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
j += 1
i += 1
print()
# 方法二:for循环
for i in range(1,10):
for k in range(9-i,0,-1):
print(7*" ",end="")
for j in range(1,i+1):
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
print()
# 方法一:while循环
i = 9
while i >= 1:
k = 9 - i
while k > 0:
print(7*" ",end="")
k -= 1
j = 1
while j <= i:
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
j += 1
i -= 1
print()
# 方法二:for循环
for i in range(9,0,-1):
for k in range(9-i,0,-1):
print(7*" ",end="")
for j in range(1,i+1):
num = "%d*%d=%2d " %(i,j,i*j)
print(num,end="")
print()
# 方法一:while循环,运算算法
i = 100
while i <= 999:
bai = i // 100
shi = i // 10 % 10
ge = i % 10
if bai == shi and shi == ge:
print(i)
elif bai+1 == shi and shi+1 == ge:
print(i)
elif bai-1 == shi and shi-1 == ge:
print(i)
i += 1
# 方法二:while循环,字符串算法
i = 100
while i <= 999:
bai = i // 100
shi = i // 10 % 10
ge = i % 10
strvar = str(i)
if strvar[0] == strvar[1] and strvar[1] == strvar[-1]:
print(i)
elif bai+1 == shi and shi+1 == ge:
print(i)
elif bai-1 == shi and shi-1 == ge:
print(i)
i += 1
# 方法三:for循环
for i in range(100,1000):
bai = i // 100
shi = i // 10 % 10
ge = i % 10
if bai == shi and shi == ge:
print(i)
elif bai+1 == shi and shi+1 == ge:
print(i)
elif bai-1 == shi and shi-1 == ge:
print(i)
公鸡 母鸡 小鸡
公鸡1块钱1只,母鸡3块钱一只,小鸡5毛钱一只
问: 用100块钱买100只鸡,有多少种买法?
# 方法一:while循环
count = 0
x = 0
while x <= 100:
y = 0
while y <= 33:
z = 0
while z <= 100:
if x+y+z == 100 and x+3*y+0.5*z == 100:
count += 1
z += 1
y += 1
x += 1
print(count)
# 方法二:for循环
count = 0
for x in range(0,101):
for y in range(0,34):
for z in range(0,101):
if x+y+z == 100 and x+3*y+0.5*z == 100:
count += 1
print(count)