python基础语法以及基础编程记录:
- 隔列打印星星:
#单个循环打印星星:
i = 0
while i < 100:
#此处执行打印星星的命令:
if i % 2 == 0:
print("☆", end = " ")
else:
print("★", end = " ")
#此处执行换行的命令:
if i % 10 == 9:
print()
i += 1
#双循环打印小星星偷懒之一:
j = 0
while j < 5:
i = 0
while i < 5:
print("♡♥", end="")
i += 1
print()
j += 1
#双循环打印小星星之二:
- 隔行打印星星:
#我刚开始想的单循环打印隔行小星星:
i = 0
j = 0
while i < 100:
if i // 10 == j and j % 2 == 0:
print("★", end = "")
else:
print("☆", end = "")
if i % 10 == 9:
print()
i += 1
if i % 10 == 0:
j += 1
#稍微优化了一下的代码写出的隔行小星星:
i = 0
while i < 100:
#隔行打印星星
if i // 10 % 2 == 0:
print("★", end = "")
else:
print("☆", end = "")
#换行:
if i % 10 == 9:
print()
i += 1
#双重循环的隔行小星星:
j = 0
while j < 10:
if j % 2 == 0:
i = 0
while i < 10:
print("♡", end="")
i += 1
else:
i = 0
while i < 10:
print("♥", end="")
i += 1
print()
j += 1
#稍微聪明一点点的代码:
j = 0
while j < 10:
i = 0
while i < 10:
if j % 2 == 0:
print("♡", end = "")
else:
print("♥", end = "")
i += 1
print()
j += 1
- 打印99乘法表:
# 99乘法表:
# 1. 正序:
i = 1
while i < 10:
j = 1
while j <= i:
print("%d * %d = %-2d" % (i, j, i*j), end = " ")
j += 1
print()
i += 1
# for 循环
for i in range(1, 10):
for j in range(1, i+1):
print("%d * %d = %-2d" % (i, j, i * j), end = " ")
print()
# 2. 倒序:
i = 1
while i < 10:
j = i
while j < 10:
print("%d * %d = %-2d" % (i, j, i * j), end=" ")
j += 1
print()
i += 1
#for 循环:
for i in range(1, 10):
for j in range(i, 10):
print("%d * %d = %-2d" % (i, j, i * j), end = " ")
print()
# 2. 空格推正序:
i = 1
while i < 10:
j = i
while j < 9:
print(end = " ")
j += 1
j = 1
while j <= i:
print("%d * %d = %-2d" % (i, j, i * j), end = " ")
j += 1
print()
i += 1
# 空格推倒序:
i = 1
while i < 10:
j = 10 - i
while j < 9:
print(end = " ")
j += 1
j = i
while j < 10:
print("%d * %d = %-2d" % (i, j, i * j), end = " ")
j += 1
print()
i += 1
效果如下:
- 打印吉利数字 形如111 222 333 123 321 234 456 654 432 这种的数字 重复或者升序或者降序:
#没错 三个循环 时间复杂度最高 俺写的(╥╯^╰╥)
i = 1
while i < 10:
j = 0
while j < 10:
k = 0
while k < 10:
if i == j and j == k:
print("%d%d%d" % (i, j, k), end = " ")
if i - j == 1 and j - k == 1:
print("%d%d%d" % (i, j, k), end = " ")
if k -j == 1 and j - i == 1:
print("%d%d%d" % (i, j, k), end = " ")
k += 1
j += 1
print()
i += 1
#参考比我厉害的大佬写的, 利用地板除, 取余, 分别把个位, 十位, 百位取出来
i = 100
while i < 1000:
gewei = i % 10
shiwei = i // 10 % 10
baiwei = i // 100
if gewei == shiwei and shiwei == baiwei:
print(i, end = " ")
if shiwei - gewei == 1 and baiwei - shiwei == 1:
print(i, end = " ")
if gewei - shiwei == 1 and shiwei - baiwei == 1:
print(i, end = " ")
i += 1
#另外一种版本, 先转换成字符串, 然后在强转回来, 进行比较(大佬还是牛逼的):
i = 100
while i < 1000:
num = str(i)
gewei = int(num[-1])
shiwei = int(num[-2])
baiwei = int(num[0]) '强转'
if gewei == shiwei and baiwei == shiwei:
print(i, end = " ")
if gewei - shiwei == 1 and shiwei - baiwei == 1:
print(i, end=" ")
if shiwei - gewei == 1 and baiwei - shiwei == 1:
print(i, end=" ")
i += 1
- 百钱百鸡, 1块钱的公鸡, 3块钱的母鸡, 5毛钱的小鸡仔, 刚好100块买100只鸡:
#干啥啥不行 穷举第一名
#没有什么是我穷举解决不了的, 如果有, 那一定是时间复杂度
i = 0
cnt = 0
while i <= 100:
j = 0
while j < 34 :
k = 100 - i - j
if i + 3 * j + 0.5 * k == 100.0:
cnt += 1
j += 1
i += 1
print(cnt)