打印九九乘法表:
外层控制行数,内层控制列数。
for i in range(1,10):
j = 1
while j <= i:
print(i,'*',j,'=',(i*j),' ',end=' ')
j += 1
print('\n')
打印菱形
思路:分成两层从中间,这个想法很low。
i = 9
m = i // 2
t = m
while t != 0:
print(t*' ','*' * ( i - 2 * t),t*' ')
t -= 1
t = 1
while t != m:
t += 1
print(t*' ','*' * ( i - 2 * t),t*' ')
方法二:
对称性
m = 7
for i in range(-m//2,m//+1):
if i < 0:
print(-i*' ','*'*(i*2+m))
else:
print(i*' ','*'*(m-i*2))
打印斐波那契数列:
思路:规律性的,通项公式。
i = 0
j = 1
m = 0
while m < 100:
m = i + j
i = j
j = m
print(m)
打印斐波那契数列第101项:
打印斐波那契数列第101项:
i = 0
j = 1
m = 0
c = 0
while c < 101 :
m = i + j
i = j
j = m
c = c + 1
else:
print(m,c)